SIGNAL
Syntax
SIGNAL
empties the diagnostics area and produces a custom error. This statement can be used anywhere, but is generally useful when used inside a stored program. When the error is produced, it can be caught by a HANDLER. If not, the current stored program, or the current statement, will terminate with the specified error.
Sometimes an error HANDLER just needs to SIGNAL the same error it received, optionally with some changes. Usually the RESIGNAL statement is the most convenient way to do this.
error_condition
can be an SQLSTATE value or a named error condition defined via DECLARE CONDITION. SQLSTATE must be a constant string consisting of five characters. These codes are standard to ODBC and ANSI SQL. For customized errors, the recommended SQLSTATE is '45000'. For a list of SQLSTATE values used by MariaDB, see the MariaDB Error Codes page. The SQLSTATE can be read via the API method mysql_sqlstate( )
.
To specify error properties user-defined variables and local variables can be used, as well as character set conversions (but you can't set a collation).
The error properties, their type and their default values are explained in the diagnostics area page.
Errors
If the SQLSTATE is not valid, the following error like this will be produced:
If a property is specified more than once, an error like this will be produced:
If you specify a condition name which is not declared, an error like this will be produced:
If MYSQL_ERRNO is out of range, you will get an error like this:
Examples
Here's what happens if SIGNAL is used in the client to generate errors:
How to specify MYSQL_ERRNO and MESSAGE_TEXT properties:
The following code shows how to use user variables, local variables and character set conversion with SIGNAL:
How to use named error conditions:
In this example, we'll define a HANDLER for an error code. When the error occurs, we SIGNAL a more informative error which makes sense for our procedure:
See Also
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?