DECLAREcondition_name
CONDITION FORcondition_value
condition_value
: {mysql_error_code
| SQLSTATE [VALUE]sqlstate_value
}
The DECLARE ... CONDITION
statement declares a named error condition, associating a name with a condition that needs specific handling. DECLARE ... CONDITION
语句声明一个命名错误条件,将名称与需要特定处理的条件相关联。The name can be referred to in a subsequent 名称可以在随后的DECLARE ... HANDLER
statement (see Section 13.6.7.2, “DECLARE ... HANDLER Statement”).DECLARE ... HANDLER
语句中引用(请参阅第13.6.7.2节,“DECLARE ... HANDLER语句”)。
Condition declarations must appear before cursor or handler declarations.条件声明必须出现在游标或处理程序声明之前。
The condition_value
for DECLARE ... CONDITION
indicates the specific condition or class of conditions to associate with the condition name. DECLARE ... CONDITION
的condition_value
指示要与条件名称关联的特定条件或条件类别。It can take the following forms:它可以采取以下形式:
mysql_error_code
: An integer literal indicating a MySQL error code.表示MySQL错误代码的整数文本。
Do not use MySQL error code 0 because that indicates success rather than an error condition. 不要使用MySQL错误代码0,因为这表示成功,而不是错误条件。For a list of MySQL error codes, see Server Error Message Reference.有关MySQL错误代码的列表,请参阅服务器错误消息参考。
SQLSTATE [VALUE] sqlstate_value
: A 5-character string literal indicating an SQLSTATE value.表示SQLSTATE值的5个字符的字符串文字。
Do not use SQLSTATE values that begin with 不要使用以'00'
because those indicate success rather than an error condition. '00'
开头的SQLSTATE值,因为这些值表示成功,而不是错误条件。For a list of SQLSTATE values, see Server Error Message Reference.有关SQLSTATE值的列表,请参阅服务器错误消息参考。
Condition names referred to in SIGNAL
or use RESIGNAL
statements must be associated with SQLSTATE values, not MySQL error codes.SIGNAL
或使用RESIGNAL
语句中引用的条件名称必须与SQLSTATE值关联,而不是与MySQL错误代码关联。
Using names for conditions can help make stored program code clearer. 使用条件名称有助于使存储程序代码更清晰。For example, this handler applies to attempts to drop a nonexistent table, but that is apparent only if you know that 1051 is the MySQL error code for “unknown table”:例如,此处理程序适用于删除不存在的表的尝试,但只有当您知道1051是“unknown table”的MySQL错误代码时,这才是明显的:
DECLARE CONTINUE HANDLER FOR 1051 BEGIN -- body of handler END;
By declaring a name for the condition, the purpose of the handler is more readily seen:通过为条件声明名称,可以更容易地看到处理程序的用途:
DECLARE no_such_table CONDITION FOR 1051; DECLARE CONTINUE HANDLER FOR no_such_table BEGIN -- body of handler END;
Here is a named condition for the same condition, but based on the corresponding SQLSTATE value rather than the MySQL error code:以下是相同条件的命名条件,但基于相应的SQLSTATE值,而不是MySQL错误代码:
DECLARE no_such_table CONDITION FOR SQLSTATE '42S02'; DECLARE CONTINUE HANDLER FOR no_such_table BEGIN -- body of handler END;