SQL Programming Manual for Pascal
Error and Status Processing
HP NonStop SQL Programming Manual for Pascal—528614-001
6-4
Using the WHENEVER Directive
You can specify a WHENEVER directive anywhere in your program. The Pascal
compiler inserts statements that perform run-time checking on the SQLCODE variable
for all statements in listing order after the WHENEVER directive appears.
The WHENEVER directive remains active for all statements that follow it in listing order
until the program disables it. For example, if you specify a WHENEVER directive in
one procedure and do not disable it, the WHENEVER directive will still be in effect
even after the program has exited the procedure. It is therefore helpful to disable
WHENEVER directives at the end of each procedure.
Table 6-1 shows the Pascal compiler pseudocode used to check the SQLCODE
variable and the order in which the checks are made.
Each WHENEVER directive applies independently to the SQL statements following it.
For example, specifying a WHENEVER SQLWARNING directive does not affect the
checking for a WHENEVER SQLERROR or a WHENEVER NOT FOUND directive.
Enabling and Disabling WHENEVER Checking
To use the WHENEVER SQLERROR directive you must be able to apply one error
handler to all SQL errors, or you must disable the WHENEVER directive for parts of
your program that you do not want the directive to process. 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 enables and disables condition checking for the WHENEVER directive:
EXEC SQL
WHENEVER SQLERROR CALL :ERROR_PROC ; { Enables checking }
...
Note. If you are debugging your program and you use WHENEVER 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.
Table 6-1. Pascal Compiler Pseudocode for Checking SQLCODE
Order Condition Compiler Pseudocode
1 NOT FOUND IF SQLCODE = 100 THEN action-
specification;
2 SQLERROR IF SQLCODE < 0 THEN action-
specification;
3 SQLWARNING IF SQLCODE > 0 AND SQLCODE <> 100 THEN
action-specification;
action-specification is one of:
CALL
host identifier;
GOTO
host identifier;
GO TO
host identifier;
CONTINUE;