EXECUTE IMMEDIATE
Prepare and run a dynamic SQL statement in one step. This command simplifies the process by combining the PREPARE and EXECUTE operations.
Syntax
EXECUTE IMMEDIATE statement
[USING param[, param] ...]
param:
expression | IGNORE | DEFAULTDescription
EXECUTE IMMEDIATE executes a dynamic SQL statement created on the fly, which can reduce performance overhead. For example:
EXECUTE IMMEDIATE 'SELECT 1'which is shorthand for:
PREPARE stmt FROM "select 1";
EXECUTE stmt;
DEALLOCATE PREPARE stmt;EXECUTE IMMEDIATE supports complex expressions as prepare source and parameters:
EXECUTE IMMEDIATE CONCAT('SELECT COUNT(*) FROM ', 't1', ' WHERE a=?') USING 5+5;Limitations: subselects and stored function calls are not supported as a prepare source.
The following examples return an error:
One can use a user or an SP variable as a workaround:
EXECUTE IMMEDIATE supports user variables and SP variables as OUT parameters:
Similar to PREPARE, EXECUTE IMMEDIATE is allowed in stored procedures but is not allowed in stored functions.
This example uses EXECUTE IMMEDIATE inside a stored procedure:
This script returns an error:
EXECUTE IMMEDIATE can use DEFAULT and IGNORE indicators as bind parameters:
EXECUTE IMMEDIATE increments the Com_execute_immediate status variable, as well as the Com_stmt_prepare, Com_stmt_execute and Com_stmt_close status variables.
Note, EXECUTE IMMEDIATE does not increment the Com_execute_sql status variable. Com_execute_sql is used only for PREPARE..EXECUTE.
This session screenshot demonstrates how EXECUTE IMMEDIATE affects status variables:
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

