When Db2 executes an SQL statement, it returns information about the statement execution. This information includes the SQL return code (SQLCODE) and the SQLSTATE, which indicate whether statement execution was successful.
An application program that contains executable SQL statements must do one of the following things:
- Declare SQLCODE and SQLSTATE (SQLCOD and SQLSTA in Fortran) as stand-alone host variables. If you specify the STDSQL(YES) precompiler or SQL statement coprocessor option, these host variables receive the return codes, and you should not include an SQLCA in your program.
- Provide a structure named SQLCA. The SQLCA includes the SQLCODE and SQLSTATE, in addition to other status information. You can provide an SQLCA by using the INCLUDE SQLCA statement. You can call the DSNTIAR sample subroutine from your application program to format the contents of the SQLCA, or you can retrieve fields from the SQLCA yourself.
In this section, the italicized phrases in the SQL return code message text corresponds to the tokens that are returned in the SQLERRMC field of the SQLCA. If Db2 returns several tokens, they appear sequentially in SQLERRMC in the order that they appear in the message text. At most, 70 bytes are returned for a message token because the SQLERRMC field of the SQLCA is limited to 70 bytes. Use the GET DIAGNOSTICS statement with a DB2_ORDINAL_TOKEN_n keyword to obtain the full value of a message token.
In some cases a token is not applicable or available for a specific instance of an error, and *N is returned instead.
Tokens might sometimes refer to the names of objects on accelerators instead of the names of associated objects on Db2.
Regardless of whether the application program provides an SQLCA or a stand-alone variable, SQLCODE is set by Db2 after each SQL statement is executed.
- If SQLCODE = 0, execution was successful.
- If SQLCODE = 100, "no data" was found. For example, a FETCH statement returned no data because the cursor was positioned after the last row of the result table.
- If SQLCODE > 0 and not = 100, execution was successful with a warning.
- If SQLCODE < 0, execution was not successful.
SQLSTATE is also set by Db2 after the execution of each SQL statement. Thus, application programs can check the execution of SQL statements by testing SQLSTATE instead of SQLCODE. SQLSTATE (SQLSTT in FORTRAN) is a 5-byte character string variable in the SQLCA.
SQLSTATE provides application programs with common codes for common error conditions (the values of SQLSTATE are product-specific only if the error or warning is product-specific). Furthermore, SQLSTATE is designed so that application programs can test for specific errors or classes of errors. The coding scheme is the same for all IBM® relational database products.