Skip navigation links

MySQL Forums :: Microsoft SQL Server :: Calling stored procedures from other stored procedures. Possible?


Advanced Search

Re: Calling stored procedures ... Dynamic SQL
Posted by: Rich Stefkovich ()
Date: October 04, 2005 03:24PM

You CAN execute Dynamic SQL from a mysql stored procedure using prepared statements (from correspondence with MySQL this might not 'officially' be supported but it works with the release candidate).

Here's a couple ways to handle a stored procedure which uses an input parameter in a prepared statement.

-- Concatenate SQL statement and input parameters before PREPARE

DELIMITER \\

DROP PROCEDURE IF EXISTS `sp_test_dynamic_sql`\\
CREATE PROCEDURE `sp_test_dynamic_sql` (_min int, _max int)
BEGIN

SET @s = CONCAT('SELECT * FROM authors WHERE id BETWEEN ', _min, ' AND ', _max);

PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

END\\

DELIMITER ;



-- PREPAREd statement executed with input variables. You must assign the input variables to a User Variable (manual section 9.3), the PREPARE statement does not seem to work with input or other DECLAREd variables (at this time?).

DELIMITER \\

DROP PROCEDURE IF EXISTS `sp_test_dynamic_sql`\\
CREATE PROCEDURE `sp_test_dynamic_sql` (_min int, _max int)
BEGIN

SET @s = 'SELECT * FROM authors WHERE id BETWEEN ? AND ?';
SET @min = _min;
SET @max = _max;

PREPARE stmt1 FROM @s;
EXECUTE stmt1 USING @min, @max;
DEALLOCATE PREPARE stmt1;

END\\

DELIMITER ;


-- Here's some SQL to create an 'authors' table to test the above stored procedures.

CREATE TABLE authors
(
id int AUTO_INCREMENT PRIMARY KEY,
name varchar(20)
);

INSERT INTO authors(name) VALUES ('Rich');
INSERT INTO authors(name) VALUES ('Mary');
INSERT INTO authors(name) VALUES ('Jane');
INSERT INTO authors(name) VALUES ('Steve');
INSERT INTO authors(name) VALUES ('Norman');

Options: ReplyQuote


Subject Written By Posted
Calling stored procedures from other stored procedures. Possible? Thomas De Scheemaeker 08/25/2005 03:32PM
Re: Calling stored procedures from other stored procedures. Possible? Roland Bouman 08/25/2005 05:03PM
Re: Calling stored procedures from other stored procedures. Possible? Thomas De Scheemaeker 08/26/2005 02:11AM
Re: Calling stored procedures from other stored procedures. Possible? Roland Bouman 08/26/2005 02:18AM
Re: Calling stored procedures from other stored procedures. Possible? Thomas De Scheemaeker 08/26/2005 03:56AM
Re: Calling stored procedures ... Dynamic SQL Rich Stefkovich 10/04/2005 03:24PM
Re: Calling stored procedures ... Dynamic SQL Roland Bouman 10/04/2005 06:27PM
Re: Calling stored procedures ... Dynamic SQL Perry Way 02/11/2006 12:13PM
Re: Calling stored procedures ... Dynamic SQL Roland Bouman 02/11/2006 01:04PM
Re: Calling stored procedures ... Dynamic SQL Perry Way 02/11/2006 01:27PM
Re: Calling stored procedures ... Dynamic SQL Roland Bouman 02/11/2006 02:47PM
Re: Calling stored procedures ... Dynamic SQL Perry Way 02/11/2006 06:32PM
Re: Calling stored procedures ... Dynamic SQL Roland Bouman 02/12/2006 03:11AM
Re: Calling stored procedures ... Dynamic SQL Perry Way 02/12/2006 01:00PM
Re: Calling stored procedures ... Dynamic SQL Roland Bouman 02/12/2006 02:20PM
Re: Calling stored procedures ... Dynamic SQL MuraliDharan V 06/08/2006 01:07AM
Re: Calling stored procedures ... Dynamic SQL Roland Bouman 06/08/2006 05:26AM
Re: Calling stored procedures ... Dynamic SQL Michael Joss 03/08/2006 11:53PM
Re: Calling stored procedures from other stored procedures. Possible? Pallavi darla 06/15/2006 10:46AM
Re: Calling stored procedures from other stored procedures. Possible? Roland Bouman 06/16/2006 04:32AM
Re: Calling stored procedures from other stored procedures. Possible? smita gore 03/30/2006 10:29PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.