SQL Programming Manual for TAL
Error and Status Processing
HP NonStop SQL Programming Manual for TAL—527887-001
6-5
Using the WHENEVER Directive
•
A WHENEVER directive does not affect SQL statements if they appear in the 
program before the WHENEVER directive.
•
If you are debugging a program and you use a WHENEVER directive to call an 
error handling procedure, you might need to save the SQLCODE value in a local 
variable within the error handling procedure. Each subsequent SQL statement 
resets SQLCODE, and you might lose a value you need to debug your program.
Enabling and Disabling the WHENEVER Directive
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 the 
SQLCODE variable after an SQL statement instead of using WHENEVER 
SQLERROR.
This example shows how to enable and disable condition checking for the 
WHENEVER directive:
EXEC SQL
WHENEVER SQLERROR CALL :error^proc; ! Enables checking
...
EXEC SQL
WHENEVER SQLERROR; ! Disables checking
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 when the:
•
SQLERROR condition executes a statement that generates an error
•
SQLWARNING condition executes a statement that generates a warning
•
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 each condition. Figure 6-2
 shows an example that enables 
and disables the directive.










