Error information in the SQLCODE, SQLSTATE, and SQLWARN fields

Error information is returned in the SQLCODE and SQLSTATE fields of the SQLCA structure, which is updated after every executable SQL statement and most database manager API calls. The SQLWARN field contains an array of warning indicators, even if SQLCODE is zero.

A source file containing executable SQL statements can provide at least one SQLCA structure with the name sqlca. The SQLCA structure is defined in the SQLCA include file. Source files without embedded SQL statements, but calling database manager APIs, can also provide one or more SQLCA structures, but their names are arbitrary.

If your application is compliant with the FIPS 127-2 standard, you can declare the SQLSTATE and SQLCODE as host variables for C, C++, COBOL, and FORTRAN applications, instead of using the SQLCA structure.

An SQLCODE value of 0 means successful execution (with possible SQLWARN warning conditions). A positive value means that the statement was successfully executed but with a warning, as with truncation of a host variable. A negative value means that an error condition occurred.

An additional field, SQLSTATE, contains a standardized error code consistent across other IBM® database products and across SQL92-conformant database managers. Practically speaking, you should use SQLSTATE values when you are concerned about portability since SQLSTATE values are common across many database managers.

The first element of the SQLWARN array, SQLWARN0, contains a blank if all other elements are blank. SQLWARN0 contains a W if at least one other element contains a warning character.

Note: If you want to develop applications that access various IBM RDBMS servers you should:
  • Where possible, have your applications check the SQLSTATE rather than the SQLCODE.
  • If your applications will use Db2 Connect, consider using the mapping facility provided by Db2 Connect to map SQLCODE conversions between unlike databases.
Note: The occurrence of an error with an accompanying SQLSTATE value of class code 40 indicates that the error has resulted in the rollback of the transaction and the release of any locks held by the transaction. Errors occurring with other SQLSTATE class code values will not result in a rollback of the transaction nor the release of locks held by the transaction. See SQLSTATE Messages.