Syntax\n------\n\nEXECUTE IMMEDIATE statement\n    [USING param[, param] ...]\n\nparam:\n    expression | IGNORE | DEFAULT\n\nDescription\n-----------\n\nEXECUTE IMMEDIATE executes a dynamic SQL statement created on the fly, which can reduce performance overhead. For example:\n\n``sql\nEXECUTE IMMEDIATE 'SELECT 1'\n`\n\nwhich is shorthand for:\n\n`sql\nPREPARE stmt FROM "select 1";\nEXECUTE stmt;\nDEALLOCATE PREPARE stmt;\n`\n\nEXECUTE IMMEDIATE supports complex expressions as prepare source and parameters:\n\n`sql\nEXECUTE IMMEDIATE CONCAT('SELECT COUNT() FROM ', 't1', ' WHERE a=?') USING 5+5;\n`\n\nLimitations: subselects and stored function calls are not supported as a prepare source.\n\nThe following examples return an error:\n\n`sql\nCREATE OR REPLACE FUNCTION f1() RETURNS VARCHAR(64) RETURN 'SELECT  FROM t1';\nEXECUTE IMMEDIATE f1();\nERROR 1970 (42000): EXECUTE IMMEDIATE does not support subqueries or stored functions\n\nEXECUTE IMMEDIATE (SELECT 'SELECT  FROM t1');\nERROR 1064 (42000): You have an error in your SQL syntax; check the manual that \n  corresponds to your MariaDB server version for the right syntax to use near \n  'SELECT 'SELECT  FROM t1')' at line 1\n\nCREATE OR REPLACE FUNCTION f1() RETURNS INT RETURN 10;\nEXECUTE IMMEDIATE 'SELECT  FROM t1 WHERE a=?' USING f1();\nERROR 1970 (42000): EXECUTE..USING does not support subqueries or stored functions\n\nEXECUTE IMMEDIATE 'SELECT  FROM t1 WHERE a=?' USING (SELECT 10);\nERROR 1064 (42000): You have an error in your SQL syntax; check the manual that \n  corresponds to your MariaDB server version for the right syntax to use near \n  'SELECT 10)' at line 1\n`\n\nOne can use a user or an SP variable as a workaround:\n\n`sql\nCREATE OR REPLACE FUNCTION f1() RETURNS VARCHAR(64) RETURN 'SELECT  FROM t1';\nSET @stmt=f1();\nEXECUTE IMMEDIATE @stmt;\n\nSET @stmt=(SELECT 'SELECT 1');\nEXECUTE IMMEDIATE @stmt;\n\nCREATE OR REPLACE FUNCTION f1() RETURNS INT RETURN 10;\nSET @param=f1();\nEXECUTE IMMEDIATE 'SELECT  FROM t1 WHERE a=?' USING @param;\n\nSET @param=(SELECT 10);\nEXECUTE IMMEDIATE 'SELECT  FROM t1 WHERE a=?' USING @param;\n`\n\nEXECUTE IMMEDIATE supports user variables and SP variables as OUT parameters:\n\n`sql\nDELIMITER $$\nCREATE OR REPLACE PROCEDURE p1(OUT a INT)\nBEGIN\n  SET a:= 10;\nEND;\n$$\nDELIMITER ;\nSET @a=2;\nEXECUTE IMMEDIATE 'CALL p1(?)' USING @a;\nSELECT @a;\n+------+\n| @a   |\n+------+\n|   10 |\n+------+\n`\n\nSimilar to PREPARE, EXECUTE IMMEDIATE is allowed in stored procedures but is not allowed in stored functions.\n\nThis example uses EXECUTE IMMEDIATE inside a stored procedure:\n\n`sql\nDELIMITER $$\nCREATE OR REPLACE PROCEDURE p1()\nBEGIN\n  EXECUTE IMMEDIATE 'SELECT 1';\nEND;\n$$\nDELIMITER ;\nCALL p1;\n+---+\n| 1 |\n+---+\n| 1 |\n+---+\n`\n\nThis script returns an error:\n\n`sql\nDELIMITER $$\nCREATE FUNCTION f1() RETURNS INT\nBEGIN\n  EXECUTE IMMEDIATE 'DO 1';\n  RETURN 1;\nEND;\n$$\nERROR 1336 (0A000): Dynamic SQL is not allowed in stored function or trigger\n`\n\nEXECUTE IMMEDIATE can use DEFAULT and IGNORE indicators as bind parameters:\n\n`sql\nCREATE OR REPLACE TABLE t1 (a INT DEFAULT 10);\nEXECUTE IMMEDIATE 'INSERT INTO t1 VALUES (?)' USING DEFAULT;\nSELECT  FROM t1;\n+------+\n| a    |\n+------+\n|   10 |\n+------+\n`\n\nEXECUTE IMMEDIATE increments the Com_execute_immediate status variable, as well as the Com_stmt_prepare, Com_stmt_execute and Com_stmt_close status variables.\n\nNote, EXECUTE IMMEDIATE does not increment the Com_execute_sql status variable. _Com_execute_sql_ is used only for PREPARE..EXECUTE.\n\nThis session screenshot demonstrates how EXECUTE IMMEDIATE affects status variables:\n\n`sql\nSELECT  FROM INFORMATION_SCHEMA.SESSION_STATUS WHERE VARIABLE_NAME RLIKE \n  ('COM_(EXECUTE|STMT_PREPARE|STMT_EXECUTE|STMT_CLOSE)');\n\n+-----------------------+----------------+\n| VARIABLE_NAME         | VARIABLE_VALUE |\n+-----------------------+----------------+\n| COM_EXECUTE_IMMEDIATE | 0              |\n| COM_EXECUTE_SQL       | 0              |\n| COM_STMT_CLOSE        | 0              |\n| COM_STMT_EXECUTE      | 0              |\n| COM_STMT_PREPARE      | 0              |\n+-----------------------+----------------+\n\nEXECUTE IMMEDIATE 'SELECT 1';\n+---+\n| 1 |\n+---+\n| 1 |\n+---+\n\nSELECT  FROM INFORMATION_SCHEMA.SESSION_STATUS WHERE VARIABLE_NAME RLIKE \n  ('COM_(EXECUTE|STMT_PREPARE|STMT_EXECUTE|STMT_CLOSE)');\n+-----------------------+----------------+\n| VARIABLE_NAME         | VARIABLE_VALUE |\n+-----------------------+----------------+\n| COM_EXECUTE_IMMEDIATE | 1              |\n| COM_EXECUTE_SQL       | 0              |\n| COM_STMT_CLOSE        | 1              |\n| COM_STMT_EXECUTE      | 1              |\n| COM_STMT_PREPARE      | 1              |\n+-----------------------+----------------+\n``\n\nURL: https://mariadb.com/docs/server/reference/sql-statements/prepared-statements/execute-immediate