DECLARE CONDITION belongs to the SQL/PSM standard and is fully supported in MySQL and MariaDB. It lets you map a descriptive identifier to a specific SQLSTATE string (five-character code) or a MySQL-specific error number. You place DECLARE CONDITION statements at the top of a compound statement block (between BEGIN and executable code) in stored procedures, functions, triggers, or events. After declaration you can: - Raise the condition with SIGNAL condition_name; - Re-raise it with RESIGNAL; - Catch it with DECLARE ... HANDLER FOR condition_name. The association exists only within the block scope. If a nested block declares a condition with the same name, it overrides the outer one. The statement does not create or modify database objects and has no runtime cost until referenced. Caveats - Works only inside stored program blocks, not in ad-hoc SQL. - condition_value must be a valid SQLSTATE literal ('ABCDE') or an integer error code (1000-4999 for MySQL). - Must appear before other non-declare statements in the block.
condition_name
(Identifier) - Name you will use in SIGNAL/HANDLER statementssqlstate_value - CHAR
(5) - A valid SQLSTATE code such as '02000' or '23000'mysql_error_code- INT
- A MySQL error number such as 1062 (duplicate key)SIGNAL, RESIGNAL, DECLARE HANDLER, SQLSTATE, EXCEPTION HANDLING
MySQL 5.5 (2010) – following the SQL/PSM standard
Bind any valid five-character SQLSTATE literal or any MySQL error number between 1000 and 4999.
Put it at the top of the BEGIN block, before executable SQL statements or control flow constructs.
No. The alias is local to the block where it is declared.
You cannot drop it explicitly. It disappears automatically when the block finishes executing.