SQL/MX 3.2.1 Programming Manual for C and COBOL (H06.26+, J06.15+)

Exception Handling and Error Conditions
HP NonStop SQL/MX Release 3.2.1 Programming Manual for C and COBOL663854-005
13-15
Saving and Restoring SQLSTATE or SQLCODE
Example
This example enables and disables the WHENEVER directive:
EXEC SQL WHENEVER SQLERROR GOTO end_prog; /* enables action */
...
end_prog:
EXEC SQL WHENEVER SQLERROR CONTINUE; /* disables action */
...
Saving and Restoring SQLSTATE or SQLCODE
If you use WHENEVER SQLERROR CALL sql_error and the sql_error function
contains SQL statements, you can save and restore the value of SQLSTATE or
SQLCODE before returning from the sql_error function.
Example
This example uses the first FETCH statement returning an SQLCODE value that is not
equal to 0:
EXEC SQL WHENEVER SQLERROR CALL sql_error;
...
EXEC SQL OPEN get_by_partnum;
...
EXEC SQL FETCH get_by_partnum
INTO :hv_partnum,:hv_partdesc,:hv_price,:hv_qty_available;
...
while (SQLCODE == 0) {
if ( hv_qty_available < 1000 )
EXEC SQL UPDATE parts
SET qty_available = qty_available + 100
WHERE CURRENT OF get_by_partnum;
...
EXEC SQL FETCH get_by_partnum
INTO :hv_partnum,:hv_partdesc,:hv_price,:hv_qty_available;
}
...
void sql_error() {
long saved_sqlcode = SQLCODE;
EXEC SQL GET DIAGNOSTICS
:hv_num = NUMBER;
for (i = 1; i <= hv_num; i++) {
EXEC SQL GET DIAGNOSTICS EXCEPTION :i
:hv_sqlstate = RETURNED_SQLSTATE,
:hv_msgtxt = MESSAGE_TEXT;
...
}
SQLCODE = saved_sqlcode;
} /* end sql_error */
After the first FETCH statement returns an error, control moves to sql_error.
Because the function sql_error contains SQL statements, the SQLCODE value in
the function overwrites the global SQLCODE value. Before returning from the function,
the global value is restored so that, when control is returned to the statement following
C
C