The WHENEVER statement specifies the action to be taken when a specified exception condition occurs.
This statement can only be embedded in an application program. It is not an executable statement. It must not be specified in Java™ or REXX. See the Embedded SQL Programming book for information on handling errors in REXX.
None required.
>>-WHENEVER--+-NOT FOUND--+--+-CONTINUE---------------------+--><
+-SQLERROR---+ '-+-GOTO--+--+---+--host-label-'
'-SQLWARNING-' '-GO TO-' '-:-'
The NOT FOUND, SQLERROR, or SQLWARNING clause is used to identify the type of exception condition.
The CONTINUE or GO TO clause are used to specify the next statement to be executed when the identified type of exception condition exists.
WHENEVER statement types: There are three types of WHENEVER statements:
WHENEVER statement scope: Every executable SQL statement in a program is within the scope of one implicit or explicit WHENEVER statement of each type. The scope of a WHENEVER statement is related to the listing sequence of the statements in the program, not their execution sequence.
An SQL statement is within the scope of the last WHENEVER statement of each type that is specified before that SQL statement in the source program. If a WHENEVER statement of some type is not specified before an SQL statement, that SQL statement is within the scope of an implicit WHENEVER statement of that type in which CONTINUE is specified.
SQL does support nested programs in COBOL, C, and RPG. However, SQL does not honor normal COBOL, C, and RPG scoping rules. That is, the last WHENEVER statement specified in the program source prior to the nested procedure is still in effect for that nested program. The label referenced in the WHENEVER statement must be duplicated within that inner program. Alternatively, the inner program could specify a new WHENEVER statement.
In FORTRAN, the scope of a WHENEVER statement is limited to SQL statements within the same subprogram.
The following statements can be embedded in a COBOL program.
Example 1: Go to the label HANDLER for any statement that produces an error.
EXEC SQL WHENEVER SQLERROR GOTO HANDLER END-EXEC.
Example 2: Continue processing for any statement that produces a warning.
EXEC SQL WHENEVER SQLWARNING CONTINUE END-EXEC.
Example 3: Go to the label ENDDATA for any statement that does not return data when expected to do so.
EXEC SQL WHENEVER NOT FOUND GOTO ENDDATA END-EXEC.