SQL Programming Manual for TAL
Error and Status Processing
HP NonStop SQL Programming Manual for TAL—527887-001
6-4
Using the WHENEVER Directive
Using the WHENEVER Directive
The WHENEVER directive specifies an action that a program takes depending on the
outcome of subsequent DML, DCL, and DDL SQL statements. WHENEVER provides
tests for these conditions:
•
An error occurred.
•
A warning occurred.
•
No rows were found.
You can specify a WHENEVER directive anywhere in your program. When you specify
this directive, the TAL compiler inserts statements that perform run-time checking after
the SQL statement using the SQLCODE variable. Table 6-1
shows the TAL compiler
pseudocode used to check SQLCODE and the order in which the checks are made.
Each WHENEVER directive independently applies to the SQL statement. For example,
specifying a WHENEVER SQLWARNING directive does not affect the checking for a
WHENEVER SQLERROR or a WHENEVER NOT FOUND directive.
Determining the Scope of a WHENEVER Directive
The order that a WHENEVER directive appears in a program determines its scope.
Some points to consider are:
•
A WHENEVER directive is in effect until another WHENEVER directive for the
same condition appears.
•
If a WHENEVER directive is coded in a procedure, the directive remains in effect
outside of the procedure even if the scope of the procedure is no longer valid.
Therefore, if you do not want the directive to remain in effect, disable it at the end
of the procedure as described in the next subsections.
•
A program’s order includes any files that are copied into the program using a
SOURCE directive. If a copied file contains a WHENEVER directive, that directive
is in effect following the SOURCE directive.
Table 6-1. TAL 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 : procedure-name ;
GOTO : label-name ;
GO TO : label-name ;
CONTINUE ;