SQLSTATE is part of the SQL Standard error diagnostics framework. Every SQL statement returns a five-character alphanumeric code: the first two characters form the class (broad category) and the last three form the subclass (specific condition). A value of '00000' means successful completion, while any non-zero class indicates a warning or error. SQLSTATE values are vendor-independent, making them preferable to product-specific numeric codes. Applications can read SQLSTATE through CLI/ODBC drivers, GET DIAGNOSTICS, or language-specific interfaces, and can test for particular states in condition handlers or exception blocks to implement robust error handling. Common classes include '01' (warning), '02' (no data), '22' (data exception), and '28' (invalid authorization). Not all databases implement every code defined by the standard, but compliant engines map their internal errors to the closest SQLSTATE.
SQL-92
'00000' signals successful completion with no warnings.
Assign RETURNED_SQLSTATE via GET DIAGNOSTICS (PostgreSQL) or test it in a DECLARE HANDLER (MySQL) right after the statement you want to monitor.
No. Most databases implement the common classes but may omit less relevant subclasses. Always test on your target engine.
Yes for portability. SQLSTATE is standardized, while SQLCODE values are vendor-specific.