top of page
CerebroSQL

MySQL: 

DECLARE CONDITION

Syntax:
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. The
name can be referred to in a subsequent DECLARE ... HANDLER statement
(see [HELP 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.
It can take the following forms:

o mysql_error_code: An integer literal indicating a MySQL error code.

Do not use MySQL error code 0 because that indicates success rather
than an error condition. For a list of MySQL error codes, see Server
Error Message Reference
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference
.html).

o SQLSTATE [VALUE] sqlstate_value: A 5-character string literal
indicating an SQLSTATE value.

Do not use SQLSTATE values that begin with '00' because those
indicate success rather than an error condition. For a list of
SQLSTATE values, see Server Error Message Reference
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference
.html).

Condition names referred to in SIGNAL or use RESIGNAL statements must
be associated with SQLSTATE values, not MySQL error codes.

URL: https://dev.mysql.com/doc/refman/8.0/en/declare-condition.html

Example

bottom of page