SQL/MP Programming Manual for COBOL

Error and Status Reporting
HP NonStop SQL/MP Programming Manual for COBOL529758-003
9-7
Using the WHENEVER Directive
This table indicates the HP COBOL compiler pseudocode that checks SQLCODE and
the order in which the checks are made:
action-specification is one of:
PERFORM :host-identifier ;
GOTO :host-identifier ;
GO TO :host-identifier;
CONTINUE ;
When more than one WHENEVER condition applies to an SQL statement, SQL/MP
processes the conditions in order of precedence. For example, an SQL error and an
SQL warning can occur for the same statement, but the error condition has a higher
precedence and is processed first.
These WHENEVER directives check for the error, warning, and not-found conditions:
EXEC SQL
WHENEVER NOT FOUND PERFORM :ROW-NOT-FOUND-7000 END-EXEC.
EXEC SQL
WHENEVER SQLERROR PERFORM :SQL-ERROR-9000 END-EXEC.
EXEC SQL
WHENEVER SQLWARNING PERFORM :SQL-WARNING-8000 END-EXEC.
...
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. If you want to execute a different routine when an
error occurs, specify a new WHENEVER directive with a different PERFORM
routine.
For example, to insert a new row only when a row is not found, specify a new
WHENEVER directive:
EXEC SQL WHENEVER NOT FOUND PERFORM :INSERT-ROW END-EXEC.
The new WHENEVER directive remains in effect until it is disabled or changed.
Order Condition Compiler Pseudocode
1NOT FOUND
IF SQLCODE = 100
THEN action-specification
2 SQLERROR
IF SQLCODE < 0
THEN action-specification
3SQLWARNING
IF SQLCODE > 0 AND SQLCODE NOT = 100
THEN action-specification
Note. SQL/MP sometimes returns values other than 100 for a not-found condition. For
example, SQL error 8230 indicates that a subquery did not return any rows, and SQL error
8423 indicates that an indicator variable was not specified for a null output value.