12.19SQLSTATE

SQLSTATE code of the Firebird error in a WHEN …​ DO block

Available inPSQL

TypeCHAR(5)

Syntax

  |SQLSTATE

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

SQLSTATE is destined to replace SQLCODE. The latter is now deprecated in Firebird and will disappear in a future version.

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.

For a complete listing of SQLSTATE codes, consult the SQLSTATE Codes and Message Texts section in Appendix B, Exception Codes and Messages.

Example

   |when any
   |do
   |begin
   |  Msg = case sqlstate
   |          when '22003' then 'Numeric value out of range.'
   |          when '22012' then 'Division by zero.'
   |          when '23000' then 'Integrity constraint violation.'
   |          else 'Something bad happened! SQLSTATE = ' || sqlstate
   |        end;
   |  exception ex_custom Msg;
   |end