SQL/MX 3.2 Programming Manual for C and COBOL (H06.25+, J06.14+)
Exception Handling and Error Conditions
HP NonStop SQL/MX Release 3.2 Programming Manual for C and COBOL—663854-002
13-5
Checking the SQLCODE Variable
... /* Process the SQL error. */
} /* end process_sqlstate */
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-13.
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 want to use both SQLCODE and SQLSTATE in your program, you must declare
them within the Declare section, with the correct data type. By default, preprocessor
does not return any error if SQLCODE is declared incorrectly. However, if SQLSTATE
is not declared correctly, preprocessor returns an error.
The -w option is used to detect the missing or incorrect SQLCODE and SQLSTATE
variables. The following arguments are supported for the -w option:
Sqlcode
Sqlstate
COBOL










