DB2 Version 9.7 for Linux, UNIX, and Windows

Including SQLSTATE and SQLCODE host variables in embedded SQL applications

About this task

In the following example, the application checks the SQLCODE field of the SQLCA structure to determine whether the update was successful.

Table 1. Embedding SQL Statements in a Host Language
Language Sample Source Code
C and C++
 EXEC SQL UPDATE staff SET job = 'Clerk' WHERE job = 'Mgr';
 if ( SQLCODE < 0 )
    printf( "Update Error:  SQLCODE = 
COBOL
 EXEC SQL UPDATE staff SET job = 'Clerk' WHERE job = 'Mgr' END_EXEC.
 IF SQLCODE LESS THAN 0
    DISPLAY 'UPDATE ERROR:  SQLCODE = ', SQLCODE.
FORTRAN
 EXEC SQL UPDATE staff SET job = 'Clerk' WHERE job = 'Mgr'
 if ( sqlcode .lt. 0 ) THEN
    write(*,*) 'Update error:  sqlcode = ', sqlcode

Before you begin

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. If your application is compliant with the FIPS 127-2 standard, you can declare host variables named SQLSTATE and SQLCODE instead of explicitly declaring the SQLCA structure in embedded SQL applications.