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 COBOL523627-004
13-6
SQLCODE Values
If you do not declare SQLCODE anywhere in your program but you do not declare
SQLSTATE within a Declare Section, the language compiler returns error and
warning messages.
SQLCODE Values
After the execution of an embedded SQL statement, NonStop SQL/MX returns the
values listed in Table 13-3 to SQLCODE. The SQL message numbers described under
Using the WHENEVER Statement on page 13-8 are SQLCODE values except for the
special values 0 and 100. The SQL message numbers that indicate errors are stored
as negative numbers, and the SQL message numbers that indicate warnings are
stored as positive numbers.
Using SQLCODE
You can declare SQLCODE as a global variable at the start of each source unit that
contains embedded SQL statements. You can then use conditional statements to
check the SQLCODE variable after the execution of an SQL statement.
This example checks the SQLCODE variable for any errors or warnings after an
INSERT statement:
Examples
...
/* global C declarations */
long SQLCODE; /* Declare SQLCODE with data type long. */
...
... /* Set new_jobcode and new_jobdesc host variables. */
EXEC SQL INSERT INTO sales.job (jobcode,jobdesc)
VALUES (:new_jobcode,:new_jobdesc);
if (SQLCODE == 0) { printf ("\nValues were inserted!");
EXEC SQL COMMIT WORK; }
else process_sqlcode();
Note. You should declare SQLCODE (anywhere in your program) or declare SQLSTATE
within a Declare Section. If you are not checking SQLSTATE, do not declare SQLSTATE
because query execution requires string processing to return the SQLSTATE value. However,
because checking SQLCODE is a deprecated feature, checking SQLSTATE is recommended
as the means to detect exception conditions. Declaration of SQLSTATE is required within a
Declare Section to detect exception conditions and to avoid compiler-generated warnings.
Table 13-3. SQLCODE Values
Value Status
< 0 An error occurred.
> 0 (<>100) A warning occurred.
100 No data was found.
0 The statement completed successfully.
C