My question is pretty straight forward. Is it possible to create a procedure that calls multiple previously stored procedures such as:
CREATE PROCEDURE `CALL_A_B_C` ( )
NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER
CALL `A` ();
CALL `B`();
CALL `C`();
This code doesn't work but you get the idea. Is there a way to do this?
The answer yes it is possible. Your code for an outer stored procedure might look like this
DELIMITER $$
CREATE PROCEDURE sp_abc()
BEGIN
CALL sp_a();
CALL sp_b();
CALL sp_c();
END$$
DELIMITER
Here is SQLFiddle demo
Related
When we create a procedure it can allows to call other procedure which is run in the server. But if there have a chance to create over one or more procedures inside of a procedure in mysql 5.7.
You cannot use the CREATE PROCEDURE statement inside a procedure body.
mysql> create procedure MyProc()
begin
create procedure MyOtherProc() begin select 1; end;
end //
ERROR 1303 (2F003): Can't create a PROCEDURE from within another stored routine
I have a procedure that creates a table, is it possible to have a view (or similar) that can call the procedure then select from the table?
I've tried this:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `new_routine`(p1 INT) RETURNS int(1)
BEGIN
CALL rMergeDateFields();
RETURN 1;
END
CREATE VIEW `db`.`vIntervals` AS
SELECT new_routine(0) AS col1;
SELECT * FROM MergedData;
but I get this error
Error 1422: Explicit or implicit commit is not allowed in stored function or trigger.
Any ideas?
No, you cannot. Views are typically read-only operations; and that behavior cannot be guaranteed if stored-procedures are invoked.
Related question:
How to call Stored Procedure in a View?
Is it possible to call stored procedure in view?
Here is a canonical resource:
http://dev.mysql.com/doc/refman/5.1/en/view-updatability.html
Some views are updatable. That is, you can use them in statements such as UPDATE, DELETE, or INSERT to update the contents of the underlying table. For a view to be updatable, there must be a one-to-one relationship between the rows in the view and the rows in the underlying table. There are also certain other constructs that make a view nonupdatable.
As invoking the stored procedure cannot assure 1:1 relationships with view rows, the update is not permitted.
You can't do this from a view, but a stored procedure itself can return a result set.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `merge_and_select` ()
BEGIN
CALL rMergeDateFields();
SELECT * FROM MergeData;
END $$
If you CALL merge_and_select() the rMergeDateFields procedure will be run and then you will get the contents of the MergeData table returned to you, in one step... which sounds like what you're wanting.
This is, however, a really bad implementation, because there's no control for concurrent calls to merge_and_select(), so all kinds of things could go wrong if it's run more than once at the same time.
However, depending on what you really need rMergeDateFields() to do, it's possible that you could rewrite rMergeDateFields() to actually perform whatever work you need done and return it directly to the client without using the MergeData table using an unbounded SELECT, as shown above.
Anything you SELECT in a stored procedure without using INTO a variable is returned to the client as a response from the CALL.
i found my answer in sql server at here, but my question is about in mysql. please help me
well to create a stored procedure in mysql you do it like so...
delimiter //;
create procedure Foo()
BEGIN
Select * from videos;
END
See http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html for additional details
I am trying to make a procdure in mysql that returns me an array with the result, I used to do with the oracle ref cursor, but in mysql do not know how to proceed,
I have to pass parameters too...
Anyone know how I can do, or have an example to show me? Thank you very much...
MySQL doesn't have a refcursor like Oracle. If you are planning to write a stored procedure that returns multiple rows/result set in MySQL just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
and call sample();. It will return a result set which you can use.
There is no analog of REF CURSOR in MySQL. Stored procedures and functions allow to pass and return only scalar datata types, see the reference here - CREATE PROCEDURE and CREATE FUNCTION Syntax.
MySQL cannot operate with arrays. A workaround is to use a table (or TEMPORARY TABLE).
Also - take advantage of visual object editors and stored procedure debugger in dbForge Studio for MySQL.
I'd like to create a procedure from PHP using PDO...I can execute the procedure with PDO, I can create the procedure with the console line or with phpmyadmin, but I'm not able to create this very same procedure from PDO...
I absolutely need to be able to create this procedure from my php code (it's a setup part of a bigger program, the first step checks if the procedure exists with
SHOW PROCEDURE STATUS LIKE 'name_of_procedure'
if the procedure doesn't exists, I try (but fail) to create it (with prepare/execute couple of methods).