L'istruzione PREPARE
Contents
Sintassi
PREPARE nome_istruzione FROM istruzione_da_preparare
Spiegazione
The PREPARE
statement prepares a statement and assigns it a name,
stmt_name
, by which to refer to the statement later. Statement names
are not case sensitive. preparable_stmt
is either a string literal or a
user variable that contains the text of the statement. The text must
represent a single SQL statement, not multiple statements. Within the
statement, "?" characters can be used as parameter markers to indicate
where data values are to be bound to the query later when you execute
it. The "?" characters should not be enclosed within quotes, even if
you intend to bind them to string values. Parameter markers can be used
only where data values should appear, not for SQL keywords,
identifiers, and so forth.
If a prepared statement with the given name already exists, it is deallocated implicitly before the new statement is prepared. This means that if the new statement contains an error and cannot be prepared, an error is returned and no statement with the given name exists.
A prepared statement is executed with EXECUTE
and released
with DEALLOCATE PREPARE
.
The scope of a prepared statement is the session within which it is created. Other sessions cannot see it.
Example
create table t1 (a int,b char(10)); insert into t1 values (1,"one"),(2, "two"),(3,"three"); prepare test from "select * from t1 where a=?"; set @param=2; execute test using @param; +------+------+ | a | b | +------+------+ | 2 | two | +------+------+ set @param=3; execute test using @param; +------+-------+ | a | b | +------+-------+ | 3 | three | +------+-------+ deallocate prepare test;