13.6.7.1 DECLARE ... CONDITION Statement语句

DECLARE condition_name CONDITION FOR condition_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 ... CONDITIONcondition_value指示要与条件名称关联的特定条件或条件类别。It can take the following forms:它可以采取以下形式:

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;