How do you build and use dynamic sql in a MySQL stored procedure?
            Asked
            
        
        
            Active
            
        
            Viewed 1.5e+01k times
        
    3 Answers
127
            
            
        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.
 
    
    
        TimoSolo
        
- 7,068
- 5
- 34
- 50
- 
                    Notice that you can only execute one statment each time. View https://stackoverflow.com/a/20374657/2630035 for an easier way – Zohar Jan 18 '21 at 22:16
59
            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!
 
    
    
        Jonny
        
- 3,807
- 8
- 31
- 48
 
    
    
        Jason Stevenson
        
- 4,004
- 3
- 29
- 49
- 
                    3
- 
                    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. – Jason Stevenson Jan 11 '10 at 20:53
- 
                    1Apparently, they're studying [EXECUTE IMMEDIATE](http://dev.mysql.com/worklog/task/?id=2793) for future versions. – Álvaro González May 14 '14 at 08:12
- 
                    3@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 = ?' );` – Stefan Rogin Dec 02 '14 at 10:54
- 
                    Also doesn't work on column names (as with table names) - see below answer/above comment instead. – Danny Beckett Jul 14 '15 at 17:21
- 
                    
4
            
            
        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)
 
    
    
        Elcio
        
- 41
- 1
