SQL/MP Programming Manual for COBOL

Error and Status Reporting
HP NonStop SQL/MP Programming Manual for COBOL529758-003
9-9
Using 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.
To avoid these situations, disable the appropriate WHENEVER directive for the part of
your program that handles the condition. Example 9-2
on page 9-11 enables and
disables the WHENEVER directive.
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