DB2 10.5 for Linux, UNIX, and Windows

SQLSTATE and SQLCODE Variables in COBOL embedded SQL application

To handle errors or debug your embedded SQL application, you should test the values of the SQLCODE or SQLSTATE variable. You can return these values as output parameters or as part of a diagnostic message string, or you can insert these values into a table to provide basic tracing support.
When using the LANGLEVEL precompile option with a value of SQL92E, the following two declarations can be included as host variables:
    EXEC SQL BEGIN DECLARE SECTION END-EXEC.
    01 SQLSTATE PIC X(5). 
    01 SQLCODE  PIC S9(9) USAGE COMP. 
    .
    .
    .
    EXEC SQL END DECLARE SECTION END-EXEC.

If neither of these is specified, the SQLCODE declaration is assumed during the precompile step. The SQLCODE and SQLSTATE variables can be declared using level 01 (as shown in the previous example) or level 77. Note that when using this option, the INCLUDE SQLCA statement should not be specified.

For applications made up of multiple source files, the SQLCODE and SQLSTATE declarations can be included in each source file as shown previously.