SQL/MP Programming Manual for COBOL85
Error and Status Reporting
HP NonStop SQL/MP Programming Manual for COBOL85—429326-004
9-8
Using the WHENEVER Directive
Enabling and Disabling WHENEVER Checking
You can enable and disable the WHENEVER directive for different parts of your
program. For example, you might want to handle SQL errors by checking SQLCODE
after an SQL statement instead of using WHENEVER SQLERROR. Example 9-1
shows how to enable and disable the WHENEVER directive.
Avoiding Infinite Loops
To avoid an infinite loop if the error handling code generates errors or warning, you can
disable the WHENEVER directive within the error handling procedure. An infinite loop
can occur in these situations:
•
The SQLERROR condition executes a statement that generates an error.
•
The SQLWARNING condition executes a statement that generates a warning.
•
The NOT FOUND condition executes a statement that generates a
NOT FOUND condition.
Example 9-1. Enabling and Disabling the WHENEVER Directive
PROCEDURE-DIVISION.
0010-SET-UP.
EXEC SQL
WHENEVER SQLERROR PERFORM :9900-SQL-ERROR-HANDLER
END-EXEC.
...
9900-SQL-ERROR-HANDLER.
* Disable SQLERROR handling to prevent looping.
EXEC SQL WHENEVER SQLERROR END-EXEC.
...
EXEC SQL
INSERT INTO ERRLOG
( ..., ERRORS_SQL, ...)
VALUES ( ..., :SQLCODE-NUM, ...)
END-EXEC.
...
...
EXEC SQL
INSERT INTO ERRLOG
( ..., ERRORS_SQL, ...)
VALUES ( ..., :SQLCODE-NUM, ...)
END-EXEC.
...
* Enable SQLERROR handling for subsequent statements.
EXEC SQL
WHENEVER SQLERROR PERFORM :9900-SQL-ERROR-HANDLER
END-EXEC.
* Exit code