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-13
Using the WHENEVER Statement
Using the WHENEVER Statement
The WHENEVER declaration specifies an action that a program takes, depending on
the results of subsequent SQL statements. When you specify WHENEVER, the
SQL/MX preprocessor generates statements in your program that perform run-time
checking using the SQLSTATE variable after each SQL statement executes.
The generated statements check for these conditions:
NOT FOUND condition: No data was found. SQLSTATE is 02000, and SQLCODE
is 100.
SQLERROR condition: An SQL error occurred. SQLSTATE indicates an exception
condition as shown in Checking the SQLSTATE Variable
on page 13-1. SQLCODE
is less than zero.
SQL_WARNING condition: An SQL warning occurred. SQLSTATE does not
indicate a no-data or an error condition. SQLCODE is greater than zero and not
equal to 100.
You must specify the WHENEVER declaration in your program before the SQL
statements to which it applies. Use this general syntax:
WHENEVER { NOT FOUND | SQLERROR | SQL_WARNING }
{ CONTINUE
| GOTO host-label-identifier
| CALL C-function
| PERFORM COBOL-routine }
For complete syntax, see the WHENEVER Declaration in the SQL/MX Reference
Manual.
This example uses WHENEVER declarations to check for the NOTFOUND,
SQLERROR, and SQL_WARNING conditions.
Examples
/* global C declarations */
...
EXEC SQL WHENEVER NOT FOUND GOTO data_not_found;
EXEC SQL WHENEVER SQLERROR GOTO end_prog;
EXEC SQL WHENEVER SQL_WARNING CONTINUE;
EXEC SQL WHENEVER NOT FOUND CALL handle_nodata;
...
* global COBOL declarations
...
EXEC SQL WHENEVER NOT FOUND GOTO data-not-found END-EXEC.
EXEC SQL WHENEVER SQLERROR GOTO end_prog END-EXEC.
EXEC SQL WHENEVER SQL_WARNING CONTINUE END-EXEC.
EXEC SQL WHENEVER NOT FOUND PERFORM handle-nodata END-EXEC.
...
C
COBOL










