Added in2.5.1

    TypeCHAR(5)

    In a WHEN …​ DO error handler, the SQLSTATE context variable contains the 5-character, SQL-2003-compliant status code resulting from the statement that raised the error. Outside error handlers, SQLSTATE is always '00000'. Outside PSQL, it is not available at all.

    Note

    • Firebird does not (yet) support the syntax WHEN SQLSTATE …​ DO. You have to use WHEN ANY and test the SQLSTATE variable within the handler.

    • Each SQLSTATE code is the concatenation of a 2-character class and a 3-character subclass. Classes 00 (successful completion), 01 (warning) and 02 (no data) represent completion conditions. Every status code outside these classes is an exception. Because classes 00, 01 and 02 don’t raise an error, they won’t ever show up in the SQLSTATE variable.

    Example

    1. begin
    2. Msg = case sqlstate
    3. when '22003' then 'Numeric value out of range.'
    4. when '23000' then 'Integrity constraint violation.'
    5. else 'Something bad happened! SQLSTATE = ' || sqlstate
    6. end;