PREPARE statement prepares a
SQL statement and assigns it a name,
stmt_name, by which to refer to the
statement later. The prepared statement is executed with
EXECUTE and released with
DEALLOCATE PREPARE. For examples,
see Section 12.6, “SQL Syntax for Prepared Statements”.
Statement names are not case sensitive.
preparable_stmt is either a string
literal or a user variable that contains the text of the SQL
statement. The text must represent a single 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
quotation marks, 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.
The scope of a prepared statement is the session within which it is created, which as several implications:
A prepared statement created in one session is not available to other sessions.
When a session ends, whether normally or abnormally, its prepared statements no longer exist. If auto-reconnect is enabled, the client is not notified that the connection was lost. For this reason, clients may wish to disable auto-reconnect. See Section 17.6.14, “Controlling Automatic Reconnection Behavior”.