For the complete documentation index, see llms.txt. This page is also available as Markdown.

EXECUTE Statement

Run a previously prepared statement. This command executes the statement using the specified name, optionally supplying input parameters.

Syntax

EXECUTE [LOCAL] stmt_name
    [USING expression[, expression] ...]

Description

After preparing a statement with PREPARE, you execute it with anEXECUTE statement that refers to the prepared statement name. If the prepared statement contains any parameter markers, you must supply aUSING clause that lists expressions containing the values to be bound to the parameters. The USING clause must name exactly as many expressions as the number of parameter markers in the statement.

You can execute a given prepared statement multiple times, passing different variables to it or setting the variables to different values before each execution.

If the specified statement has not been PREPAREd, an error similar to the following is produced:

ERROR 1243 (HY000): Unknown prepared statement handler (stmt_name) given to EXECUTE

EXECUTE with arbitrary expression as parameters can be used, not just user variables (@var_name).

LOCAL Statement Names

EXECUTE LOCAL is available from MariaDB 13.1.1.

Inside a stored procedure, EXECUTE LOCAL spvar executes the prepared statement whose name is the value of the stored-procedure variable spvar, rather than a literal name. A USING clause is supported as usual. EXECUTE LOCAL is only valid inside a stored procedure, and like the plain form is not permitted in stored functions or triggers.

Example:

CREATE PROCEDURE p1()
BEGIN
  DECLARE spvar_with_ps_name VARCHAR(64) DEFAULT 'my_stmt';
  DECLARE a INT DEFAULT 5;

  PREPARE my_stmt FROM 'SELECT ?';
  EXECUTE LOCAL spvar_with_ps_name USING a;
END;

Example

See example in PREPARE.

See Also

This page is licensed: GPLv2, originally from fill_help_tables.sql

spinner

Last updated

Was this helpful?