Skip to content

EXECUTE

Grammar Description

The purpose of the EXECUTE statement is: After using PREPARE to prepare a statement, you can use the EXECUTE statement to refer to the precompiled statement name and execute it. If a precompiled statement contains any parameter tags, you must provide a USING clause that lists user variables that contain the value to bind to the parameter. Parameter values ​​can only be provided by user variables, and the USING clause must name as many variables as the number of parameter markers in the statement.

You can execute a given precompiled statement multiple times, pass different variables to it, or set the variable to a different value before each execution.

Grammar Structure

EXECUTE stmt_name
    [USING @var_name [, @var_name] ...]

Parameter definition

Parameters Description
stmt_name Name of precompiled SQL statement

Example

> CREATE TABLE numbers(pk INTEGER PRIMARY KEY, ui BIGINT UNSIGNED, si BIGINT);
> INSERT INTO numbers VALUES (0, 0, -9223372036854775808), (1, 18446744073709551615, 9223372036854775807);
> SET @si_min = -9223372036854775808;
> SET @si_max = 9223372036854775807;
> PREPARE s2 FROM 'SELECT * FROM numbers WHERE si=?';
Query OK, 0 rows affected (0.00 sec)

> EXECUTE s2 USING @si_min;
+------+-----------------------------------------------------------------------------------------------------------------------
| pk | ui | si |
+------+-----------------------------------------------------------------------------------------------------------------------
| 0 | 0 | -9223372036854775808 |
+------+-----------------------------------------------------------------------------------------------------------------------
1 row in set (0.01 sec)

> EXECUTE s2 USING @si_max;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| pk | ui | si |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | 18446744073709551615 | 9223372036854775807 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.01 sec)

> DEALLOCATE PREPARE s2;
Query OK, 0 rows affected (0.00 sec)