How To have Dynamic SQL in MySQL Stored Procedure

136,951

Solution 1

I don't believe MySQL supports dynamic sql. You can do "prepared" statements which is similar, but different.

Here is an example:

mysql> PREPARE stmt FROM 
    -> 'select count(*) 
    -> from information_schema.schemata 
    -> where schema_name = ? or schema_name = ?'
;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE stmt 
    -> USING @schema1,@schema2
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)
mysql> DEALLOCATE PREPARE stmt;

The prepared statements are often used to see an execution plan for a given query. Since they are executed with the execute command and the sql can be assigned to a variable you can approximate the some of the same behavior as dynamic sql.

Here is a good link about this:

Don't forget to deallocate the stmt using the last line!

Good Luck!

Solution 2

After 5.0.13, in stored procedures, you can use dynamic SQL:

delimiter // 
CREATE PROCEDURE dynamic(IN tbl CHAR(64), IN col CHAR(64))
BEGIN
    SET @s = CONCAT('SELECT ',col,' FROM ',tbl );
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END
//
delimiter ;

Dynamic SQL does not work in functions or triggers. See the MySQL documentation for more uses.

Solution 3

You can pass thru outside the dynamic statement using User-Defined Variables

Server version: 5.6.25-log MySQL Community Server (GPL)

mysql> PREPARE stmt FROM 'select "AAAA" into @a';
Query OK, 0 rows affected (0.01 sec)
Statement prepared

mysql> EXECUTE stmt;
Query OK, 1 row affected (0.01 sec)

DEALLOCATE prepare stmt;
Query OK, 0 rows affected (0.01 sec)

mysql> select @a;
+------+
| @a   |
+------+
|AAAA  |
+------+
1 row in set (0.01 sec)
Share:
136,951
Brian Boatright
Author by

Brian Boatright

Learn, Think, Do!

Updated on July 05, 2022

Comments

  • Brian Boatright
    Brian Boatright almost 2 years

    How do you build and use dynamic sql in a MySQL stored procedure?

  • Jader Dias
    Jader Dias over 14 years
    it isn't working when the ? is in the place of the table name
  • Jason Stevenson
    Jason Stevenson over 14 years
    Are you trying this via the mysql console? or using another method, if you are using this in code, your provider would need to support it.
  • Álvaro González
    Álvaro González about 10 years
    Apparently, they're studying EXECUTE IMMEDIATE for future versions.
  • Stefan Rogin
    Stefan Rogin over 9 years
    @JaderDias use CONCAT to create the stmt, and only use ? for non-meta information. e.g. : PREPARE stmt FROM CONCAT('SELECT ',col,' FROM ',tbl,' WHERE col = ? AND col2 = ?' );
  • Danny Beckett
    Danny Beckett almost 9 years
    Also doesn't work on column names (as with table names) - see below answer/above comment instead.
  • Zohar
    Zohar over 3 years
    Notice that you can only execute one statment each time. View stackoverflow.com/a/20374657/2630035 for an easier way