Detecting and processing error and warning conditions in host language applications

Errors and warnings conditions in host language applications can be checked by using the SQLCODE or SQLSTATE host variables or by using the SQLCA.

Each host language provides a mechanism for handling diagnostic information.

  • In Assembler, C, COBOL, Fortran, and PL/I, an application program that contains executable SQL statements must provide at least one of the following:
    • A structure named SQLCA, which can be provided by using the INCLUDE SQLCA statement
    • A stand-alone CHAR(5) (CHAR(6) in C) variable named SQLSTATE (SQLSTT in Fortran)
    • A stand-alone integer variable named SQLCODE (SQLCOD in Fortran)
  • In Java, for error conditions, the getSQLState method of the JDBC SQLException class can be used to get the SQLSTATE and the getErrorCode method can be used to get the SQLCODE.
  • In REXX, an SQLCA is provided automatically.

Whether you define stand-alone SQLCODE and SQLSTATE host variables or an SQLCA in your program depends on the DB2® precompiler option you choose.

If the application is using DB2 ODBC and it calls the SQLGetSQLCA function, it need only include an SQLCA. Otherwise, all notification of success or errors is specified with return codes for the various function calls.

When you specify STDSQL(YES), which indicates conformance to the SQL standard, you should not define an SQLCA. The stand-alone variable for SQLCODE must be a valid host variable in the DECLARE SECTION of a program. It can also be declared outside of the DECLARE SECTION when no variable is defined for SQLSTATE. The stand-alone variable for SQLSTATE must be declared in the DECLARE SECTION. It must not be declared as an element of a structure.

Use a stand-alone SQLSTATE to conform with the SQL 2003 Core standard. When you specify STDSQL(NO), which indicates conformance to DB2 rules, you must include an SQLCA explicitly to have access to the SQLSTATE and SQLCODE information.