mysqldude

Writing dynamic SQL in stored procedures

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
PREPARE / EXECUTE from a session variable (original)
original post
✓✓✓✓✓✓✓✓✓
Unquoted table name lets a caller read another table (SQL injection)
version check
✓✓✓✓✓✓✓✓✓
Quote the identifier with backticks; injection attempt now fails (1146)
modern rewrite
✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146✓ 1146
PREPARE from a local (DECLAREd) variable
version check
✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✓✓✓✓
EXECUTE IMMEDIATE
version check
✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✓✓✓✓
PREPARE / EXECUTE from a session variable (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Unquoted table name lets a caller read another table (SQL injection) version check
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Quote the identifier with backticks; injection attempt now fails (1146) modern rewrite
MySQL 5.6✓ 11465.7✓ 11468.0✓ 11468.4✓ 11469✓ 1146
MariaDB 10.6✓ 114610.11✓ 114611.4✓ 114611.8✓ 1146
PREPARE from a local (DECLAREd) variable version check
MySQL 5.6✗ 10645.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11✓11.4✓11.8✓
EXECUTE IMMEDIATE version check
MySQL 5.6✗ 10645.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (2)
  • PREPARE from a local (DECLAREd) variable (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: END' at line 5
  • EXECUTE IMMEDIATE (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONCAT('SELECT * FROM ', 't1')' at line 1

The original article

First published 2011-03-07. The text below is the original; the table above shows how it behaves on current versions.

The following is sample code which shows how dynamic SQL can be generated within a stored procedure. In the example we are simply generating a stored procedure which takes a single parameter, a table name. We dynamically access the information of that table and return the results.

For dynamic SQL we need to generate the SQL, then prepare a statement for execution (PREPARE), execute (EXECUTE) the statement and then cleanup (DEALLOCATE PREPARE).

It should be noted that the example below gives much better performance by issuing the SQL directly rather than dynamically generating the SQL for execution. Below we have a table of 10 runs for both stored procedure versus direct SQL and it does show a difference. While a single run will not show any performance differences, if this is code contained within a processing LOOP each iteration gives the 1ms difference.

DROP PROCEDURE IF EXISTS aProc;
DELIMITER $$
CREATE PROCEDURE aProc(tableName VARCHAR(30))
BEGIN
	SET @s = CONCAT('SELECT * FROM ', tableName);
	PREPARE stmt FROM @s;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
END
$$
DELIMITER ;

Here are the results of running the stored procedure and the SQL natively.

Stored Procedure
Run #1 Run #2 Run #3 Run #4 Run #5 Run #6 Run #7 Run #8 Run #9 Run #10
Real time 0.026 0.036 0.026 0.025 0.026 0.026 0.025 0.025 0.025 0.027
Native SQL call
Run #1 Run #2 Run #3 Run #4 Run #5 Run #6 Run #7 Run #8 Run #9 Run #10
Real time 0.028 0.026 0.025 0.025 0.025 0.025 0.025 0.025 0.027 0.025

Average time for stored procedure method 0.0267 seconds. Average time for native SQL 0.0256 seconds.