SQL Syntax for Prepared Statements
1134
SQL syntax for prepared statements is based on three SQL statements:
•
PREPARE
prepares a statement for execution (see
Section 13.5.1, “
PREPARE
Syntax”
).
•
EXECUTE
executes a prepared statement (see
Section 13.5.2, “
EXECUTE
Syntax”
).
•
DEALLOCATE PREPARE
releases a prepared statement (see
Section 13.5.3, “
DEALLOCATE
PREPARE
Syntax”
).
The following examples show two equivalent ways of preparing a statement that computes the
hypotenuse of a triangle given the lengths of the two sides.
The first example shows how to create a prepared statement by using a string literal to supply the text
of the statement:
mysql>
PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql>
SET @a = 3;
mysql>
SET @b = 4;
mysql>
EXECUTE stmt1 USING @a, @b;
+------------+
| hypotenuse |
+------------+
| 5 |
+------------+
mysql>
DEALLOCATE PREPARE stmt1;
The second example is similar, but supplies the text of the statement as a user variable:
mysql>
SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql>
PREPARE stmt2 FROM @s;
mysql>
SET @a = 6;
mysql>
SET @b = 8;
mysql>
EXECUTE stmt2 USING @a, @b;
+------------+
| hypotenuse |
+------------+
| 10 |
+------------+
mysql>
DEALLOCATE PREPARE stmt2;
Here is an additional example which demonstrates how to choose the table on which to perform a
query at runtime, by storing the name of the table as a user variable:
mysql>
USE test;
mysql>
CREATE TABLE t1 (a INT NOT NULL);
mysql>
INSERT INTO t1 VALUES (4), (8), (11), (32), (80);
mysql>
SET @table = 't1';
mysql>
SET @s = CONCAT('SELECT * FROM ', @table);
mysql>
PREPARE stmt3 FROM @s;
mysql>
EXECUTE stmt3;
+----+
| a |
+----+
| 4 |
| 8 |
| 11 |
| 32 |
| 80 |
+----+
mysql>
DEALLOCATE PREPARE stmt3;
A prepared statement is specific to the session in which it was created. If you terminate a session
without deallocating a previously prepared statement, the server deallocates it automatically.
A prepared statement is also global to the session. If you create a prepared statement within a stored
routine, it is not deallocated when the stored routine ends.
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...