SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
Exception Handling and Error Conditions
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
13-5
Checking the SQLCODE Variable
This example checks the value of the SQLSTATE variable only after the UPDATE 
statement. To ensure your program is executing properly, you must check SQLSTATE 
after every SQL statement. For further information on how to do this without error 
checking after every statement, see Using the WHENEVER Statement on page 13-8.
Example
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
 01 SQLSTATE PIC X(5).
 ...
EXEC SQL END DECLARE SECTION END-EXEC.
 01 SQLSTATE-OK PIC X(5) VALUE "00000".
 ...
 EXEC SQL BEGIN WORK END-EXEC.
 EXEC SQL UPDATE customer SET CREDIT = 'CR' END-EXEC. 
 IF SQLSTATE = SQLSTATE-OK 
 DISPLAY "Rows were updated!" 
 EXEC SQL COMMIT WORK END-EXEC. 
 ELSE PERFORM 1000-PROCESS-SQLSTATE.
 ...
 1000-PROCESS-SQLSTATE.
 DISPLAY "Error or warning occurred! SQLSTATE = " SQLSTATE.
* Process the SQL error
 ...
Checking the SQLCODE Variable
Although checking the SQLSTATE variable is recommended to detect exception 
conditions, NonStop SQL/MX also supports the SQLCODE variable. 
Declaring SQLCODE
In a C program, declare SQLCODE as an integer variable of type long within the 
scope of each embedded SQL statement in your program. 
In a COBOL program, declare SQLCODE as an integer variable of type S9(9) COMP. 
Declaring SQLCODE and SQLSTATE
If you declare SQLCODE or SQLSTATE (or both) in your program, these results are 
possible:
•
If you declare both SQLCODE and SQLSTATE within a Declare Section, both 
values are returned.
•
If you declare SQLCODE outside of a Declare Section and SQLSTATE within a 
Declare Section, only SQLSTATE is returned.
•
If you declare SQLCODE (anywhere in your program) but you do not declare 
SQLSTATE within a Declare Section, only SQLCODE is returned, and the 
language compiler issues a warning message. 
COBOL










