The DECLARE ... CONDITION statement is used to declare an exception condition and assign a name to it, and associate the name with the handler specified by the exception condition.
Exception condition declarations must appear before cursor or handler declarations. The syntax for the DECLARE ... CONDITION statement is as follows:
DECLARE condition_name CONDITION FOR condition_value
condition_value: {
mysql_error_code
| SQLSTATE [VALUE] sqlstate_value
}
The condition_name can be referenced in subsequent DECLARE ... HANDLER statements.
The condition_value indicates the specified condition or condition category associated with the exception condition name. It can take the following forms:
mysql_error_code: an integer literal representing the error code. Note that code 0 indicates success, not an error, so do not use 0 to represent an error code.SQLSTATE [VALUE] sqlstate_value: a 5-character string literal representing theSQLSTATEvalue. Do not useSQLSTATEvalues starting with '00', as '00' indicates success, not an error.
The exception condition name used in SIGNAL or with the RESIGNAL statement must be associated with an SQLSTATE value, not an error code.
Using condition names can make stored program code clearer. Here is an example of an exception condition based on mysql_error_code:
DECLARE unknown_column CONDITION FOR 5217;
DECLARE CONTINUE HANDLER FOR unknown_column
BEGIN
-- Handler body
END;
Here is an example of an exception condition based on an SQLSTATE value:
DECLARE unknown_column CONDITION FOR SQLSTATE '42S22';
DECLARE CONTINUE HANDLER FOR unknown_column
BEGIN
-- Handler body
END;