SQL SIGNAL is an error-handling statement defined in the SQL/PSM standard and implemented in MySQL and MariaDB. When executed inside a stored procedure, function, trigger, or event, SIGNAL immediately aborts the current statement block and returns a custom SQLSTATE along with optional diagnostic information to the client or outer handler. Because SIGNAL behaves like a server-generated error, it automatically rolls back the current statement (or the entire transaction if autocommit is on) and can be caught by DECLARE ... HANDLER blocks higher in the call stack.Developers use SIGNAL to enforce business rules, validate input, or re-throw lower-level errors with clearer messages. The statement supports both numeric SQLSTATE codes and named conditions previously declared with DECLARE CONDITION. Optional SET clauses allow population of the diagnostics area (MESSAGE_TEXT, MYSQL_ERRNO, CONSTRAINT_CATALOG, etc.).Key caveats:- SIGNAL is not available outside stored programs (you cannot run it as a standalone query in MySQL clients).- Only five-character SQLSTATE strings in the ranges '01', '02', or 'HY' (or vendor-specific '45') are allowed.- If no MESSAGE_TEXT is provided, MySQL returns a generic "Unhandled user-defined exception" message.
- sqlstate_value: CHAR
(5) - Five-character SQLSTATE return code such as '45000' (generic unhandled user exception).- condition_name
(Identifier) - A symbolic name declared earlier with DECLARE CONDITION.- condition_item_name
- One of MESSAGE_TEXT, MYSQL_ERRNO, CONSTRAINT_NAME, CONSTRAINT_SCHEMA, CONSTRAINT_CATALOG, COLUMN_NAME, CURSOR_NAME, SUBCLASS_ORIGIN.- expr
- Any SQL expression resolving to the data type required by the condition item.MySQL 5.5 (2010)
SIGNAL creates a new error condition, while RESIGNAL rethrows the error that a handler has caught, optionally modifying its diagnostics.
No. SIGNAL is legal only inside stored procedures, functions, triggers, or event scheduler bodies.
Use '45000' for generic user errors. Choose codes in class '42' for syntax issues, '22' for data exceptions, or vendor class '45' for application-defined errors.
SIGNAL rolls back the statement that triggered it. In autocommit mode this ends the transaction; otherwise you can still commit or roll back the surrounding transaction explicitly.