13.5.1 PREPARE Statement语句

PREPARE stmt_name FROM preparable_stmt

The PREPARE statement prepares a SQL statement and assigns it a name, stmt_name, by which to refer to the statement later. PREPARE语句准备一条SQL语句,并为其指定一个名称stmt_name,以便稍后引用该语句。The prepared statement is executed with EXECUTE and released with DEALLOCATE PREPARE. 准备好的语句通过EXECUTE执行,通过DEALLOCATE PREPARE释放。For examples, see Section 13.5, “Prepared Statements”.例如,请参阅第13.5节,“准备好的语句”

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. preparable_stmt是字符串文字或包含SQL语句文本的用户变量。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.参数标记只能在数据值应该出现的地方使用,不能用于SQL关键字、标识符等。

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:准备好的声明的范围是在其中创建声明的会话,包括以下几个含义:

Beginning with MySQL 8.0.22, a parameter used in a prepared statement has its type determined when the statement is first prepared, and retains this type whenever EXECUTE is invoked for this prepared statement (unless the statement is reprepared, as explained later in this section). 从MySQL 8.0.22开始,准备好的语句中使用的参数在第一次准备语句时确定其类型,并在为此准备好的语句调用EXECUTE时保留此类型(除非重新准备语句,如本节后面所述)。Rules for determining a parameter's type are listed here:此处列出了确定参数类型的规则:

For some combinations of actual type and derived type, an automatic repreparation of the statement is triggered, to ensure closer compatibility with previous versions of MySQL. 对于实际类型和派生类型的某些组合,会触发语句的自动重新编写,以确保与MySQL的早期版本更紧密地兼容。Repreparation does not occur if any of the following conditions are true:如果满足下列任一条件,则不进行重新准备:

For cases other than those just listed, the statement is reprepared and the actual parameter types are used instead of the derived parameter types.对于刚列出的以外的情况,将重新编写语句,并使用实际参数类型而不是派生参数类型。

These rules also apply to a user variable referenced in a prepared statement.这些规则也适用于准备语句中引用的用户变量。

Using a different data type for a given parameter or user variable within a prepared statement for executions of the statement subsequent to the first execution causes the statement to be reprepared. 在准备好的语句中为给定的参数或用户变量使用不同的数据类型,以便在第一次执行之后执行该语句,这会导致重新准备该语句。This is less efficient; it may also lead to the parameter's (or variable's) actual type to vary, and thus for results to be inconsistent, with subsequent executions of the prepared statement. 效率较低;它还可能导致参数(或变量)的实际类型发生变化,从而导致结果与准备语句的后续执行不一致。For these reasons, it is advisable to use the same data type for a given parameter when re-executing a prepared statement.由于这些原因,在重新执行准备好的语句时,建议对给定参数使用相同的数据类型。