DECLARE ... CONDITION
Syntax
The DECLARE ... CONDITION
statement declares a named error condition, associating a name with a condition that needs specific handling. The name can be referred to in a subsequent DECLARE ... HANDLER
statement (see , "DECLARE ... HANDLER
Syntax").
Condition declarations must appear before cursor or handler declarations.
The condition_value
for DECLARE ... CONDITION
can be a MariaDB error code (a number) or an SQLSTATE value (a 5-character string literal). You should not use MariaDB error code 0 or SQLSTATE values that begin with '00'
, because those indicate success rather than an error condition. For a list of MariaDB error codes and SQLSTATE values, see "Server Error Codes and Messages".
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 the meaning of MariaDB error code 1051:
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 MariaDB error code:
DECLARE no_such_table CONDITION FOR SQLSTATE '42S02'; DECLARE CONTINUE HANDLER FOR no_such_table BEGIN -- body of handler END;
Condition names referred to in SIGNAL
or use RESIGNAL
statements must be associated with SQLSTATE values, not MariaDB error codes.