PREPAREstmt_name
FROMpreparable_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:准备好的声明的范围是在其中创建声明的会话,包括以下几个含义:
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 Automatic Reconnection Control.请参阅自动重新连接控制。
A prepared statement created within a stored program continues to exist after the program finishes executing and can be executed outside the program later.在存储程序中创建的准备好的语句在程序完成执行后继续存在,以后可以在程序外执行。
A statement prepared in stored program context cannot refer to stored procedure or function parameters or local variables because they go out of scope when the program ends and would be unavailable were the statement to be executed later outside the program. 在存储程序上下文中准备的语句不能引用存储过程、函数参数或局部变量,因为它们在程序结束时超出了作用域,如果以后要在程序外执行的语句将不可用。As a workaround, refer instead to user-defined variables, which also have session scope; see Section 9.4, “User-Defined Variables”.作为一种解决方法,请参考用户定义的变量,这些变量也具有会话范围;参见第9.4节,“用户定义变量”。
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 从MySQL 8.0.22开始,准备好的语句中使用的参数在第一次准备语句时确定其类型,并在为此准备好的语句调用EXECUTE
is invoked for this prepared statement (unless the statement is reprepared, as explained later in this section). EXECUTE
时保留此类型(除非重新准备语句,如本节后面所述)。Rules for determining a parameter's type are listed here:此处列出了确定参数类型的规则:
A parameter which is an operand of a binary arithmetic operator has the same data type as the other operand.作为二进制算术运算符的操作数的参数与另一个操作数具有相同的数据类型。
If both operands of a binary arithmetic operator are parameters, the type of the parameters is decided by the context of the operator.如果二进制算术运算符的两个操作数都是参数,则参数的类型由运算符的上下文决定。
If a parameter is the operand of a unary arithmetic operator, the parameter's type is decided by the context of the operator.如果参数是一元算术运算符的操作数,则参数的类型由运算符的上下文决定。
If an arithmetic operator has no type-determining context, the derived type for any parameters involved is 如果算术运算符没有类型确定上下文,则涉及的任何参数的派生类型都是DOUBLE PRECISION
. DOUBLE PRECISION
的。This can happen, for example, when the parameter is a top-level node in a 例如,当参数是SELECT
list, or when it is part of a comparison operator.SELECT
列表中的顶级节点时,或当它是比较运算符的一部分时,可能会发生这种情况。
A parameter which is an operand of a character string operator has the same derived type as the aggregated type of the other operands. 作为字符串运算符的操作数的参数的派生类型与其他操作数的聚合类型相同。If all operands of the operator are parameters, the derived type is 如果运算符的所有操作数都是参数,则派生类型为VARCHAR
; its collation is determined by the value of collation_connection
.VARCHAR
;它的排序规则由collation_connection
的值决定。
A parameter which is an operand of a temporal operator has type 作为时态运算符的操作数的参数的类型为DATETIME
if the operator returns a DATETIME
, TIME
if the operator returns a TIME
, and DATE
if the operator returns a DATE
.DATETIME
(如果运算符返回DATETIME
),TIME
(如果运算符返回TIME
)和DATE(如果运算符返回DATE
)。
A parameter which is an operand of a binary comparison operator has the same derived type as the other operand of the comparison.作为二进制比较运算符的操作数的参数与比较运算符的其他操作数具有相同的派生类型。
A parameter that is an operand of a ternary comparison operator such as 作为三元比较运算符(如BETWEEN
has the same derived type as the aggregated type of the other operands.BETWEEN
)的操作数的参数的派生类型与其他操作数的聚合类型相同。
If all operands of a comparison operator are parameters, the derived type for each of them is 如果比较运算符的所有操作数都是参数,则每个操作数的派生类型都是VARCHAR
, with collation determined by the value of collation_connection
.VARCHAR
,排序规则由collation_connection
的值确定。
A parameter that is an output operand of any of 作为CASE
, COALESCE
, IF
, IFNULL
, or NULLIF
has the same derived type as the aggregated type of the operator's other output operands.CASE
、COALESCE
、IF
、IFNULL
或NULLIF
中任何一个的输出操作数的参数,其派生类型与运算符的其他输出操作数的聚合类型相同。
If all output operands of any of 如果CASE
, COALESCE
, IF
, IFNULL
, or NULLIF
are parameters, or they are all NULL
, the type of the parameter is decided by the context of the operator.CASE
、COALESCE
、IF
、IFNULL
或NULLIF
中任何一个的所有输出操作数都是参数,或者它们都为NULL
,则参数的类型由运算符的上下文决定。
If the parameter is an operand of any of of 如果参数是CASE
, COALESCE()
, IF
, or IFNULL
, and has no type-determining context, the derived type for each of the parameters involved is VARCHAR
, and its collation is determined by the value of collation_connection
.CASE
、COALESCE()
、IF
或IFNULL
中任意一个的操作数,并且没有类型确定上下文,则涉及的每个参数的派生类型都是VARCHAR
,其排序规则由collation_connection
的值确定。
A parameter which is the operand of a 作为CAST()
has the same type as specified by the CAST()
.CAST()
的操作数的参数的类型与CAST()
指定的类型相同。
If a parameter is an immediate member of a 如果参数是不是SELECT
list that is not part of an INSERT
statement, the derived type of the parameter is VARCHAR
, and its collation is determined by the value of collation_connection
.INSERT
语句一部分的SELECT
列表的直接成员,则该参数的派生类型为VARCHAR
,其排序规则由collation_connection
的值确定。
If a parameter is an immediate member of a 如果参数是作为SELECT
list that is part of an INSERT
statement, the derived type of the parameter is the type of the corresponding column into which the parameter is inserted.INSERT
语句一部分的SELECT
列表的直接成员,则该参数的派生类型是插入该参数的相应列的类型。
If a parameter is used as source for an assignment in a 如果某个参数用作SET
clause of an UPDATE
statement or in the ON DUPLICATE KEY UPDATE
clause of an INSERT
statement, the derived type of the parameter is the type of the corresponding column which is updated by the SET
or ON DUPLICATE KEY UPDATE
clause.UPDATE
语句的SET
子句或INSERT
语句的ON DUPLICATE KEY UPDATE
子句中赋值的源,则该参数的派生类型是由SET
或ON DUPLICATE KEY UPDATE
子句更新的相应列的类型。
If a parameter is an argument of a function, the derived type depends on the function's return type.如果参数是函数的参数,则派生类型取决于函数的返回类型。
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:如果满足下列任一条件,则不进行重新准备:
NULL
is used as the actual parameter value.NULL
用作实际参数值。
A parameter is an operand of a 参数是CAST()
. CAST()
的操作数。(Instead, a cast to the derived type is attempted, and an exception raised if the cast fails.)(相反,将尝试对派生类型进行转换,如果转换失败,将引发异常。)
A parameter is a string. 参数是字符串。(In this case, an implicit (在这种情况下,会执行隐式CAST(? AS
is performed.)derived_type
)CAST(? AS
。)derived_type
)
The derived type and actual type of the parameter are both 参数的派生类型和实际类型都是INTEGER
and have the same sign.INTEGER
并且具有相同的符号。
The parameter's derived type is 参数的派生类型为DECIMAL
and its actual type is either DECIMAL
or INTEGER
.DECIMAL
,其实际类型为DECIMAL
或INTEGER
。
The derived type is 派生类型是DOUBLE
and the actual type is any numeric type.DOUBLE
,实际类型是任意数字类型。
Both the derived type and the actual type are string types.派生类型和实际类型都是字符串类型。
If the derived type is temporal and the actual type is temporal. 如果派生类型是临时的,而实际类型是临时的。Exceptions: The derived type is 例外:派生类型为TIME
and the actual type is not TIME
; the derived type is DATE
and the actual type is not DATE
.TIME
,实际类型为非TIME
;派生类型是DATE
,而实际类型不是DATE
。
The derived type is temporal and the actual type is numeric.派生类型是临时的,实际类型是数字的。
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.由于这些原因,在重新执行准备好的语句时,建议对给定参数使用相同的数据类型。