SQL/MP Programming Manual for C
Error and Status Reporting
HP NonStop SQL/MP Programming Manual for C—429847-008
9-7
Using the WHENEVER Directive
Determining the Scope of a WHENEVER Directive
The order in which WHENEVER directives appear in the listing determines their scope.
Some considerations follow:
A WHENEVER directive remains in effect until another WHENEVER directive for
the same condition appears. To execute a different routine when an error occurs,
specify a new WHENEVER directive with a different CALL routine.
For example, to insert a new row only when a row is not found, specify a new
WHENEVER directive as follows:
EXEC SQL WHENEVER NOT FOUND CALL :insert_row;
The new WHENEVER directive remains in effect until it is disabled or changed.
If a WHENEVER directive is coded in a function, the directive remains in effect
outside of the function even if the scope of the function is no longer valid.
Therefore, if you do not want the directive to remain in effect, disable it at the end
of the function as described following.
A program’s order includes any files copied into the program using an include
directive. If a copied file contains a WHENEVER directive, that directive remains in
effect following the include 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 the program.
Enabling and Disabling the WHENEVER Directive
You can enable and disable the WHENEVER directive for different parts of a 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 the WHENEVER directive:
EXEC SQL
WHENEVER SQLERROR CALL :err_func; /* enables checking */
...
EXEC SQL
WHENEVER SQLERROR; /* disables checking */