WHENEVER

The WHENEVER statement (embedded SQL only) has three different forms, each of which changes the execution of a host language program according to the occurrence of specified exception conditions.
Restriction: This topic applies to Windows environments only.

Invocation

WHENEVER is not an executable command. It can only be embedded in a host language. WHENEVER cannot be dynamically prepared.

Authorization

No special type of authorization is required.

Syntax

WHENEVER {NOT FOUND | SQLERROR | SQLWARNING} 
    {CONTINUE | GO TO host-label}

Parameters:

host-label Preceded by the keywords GO TO (or GOTO), specifies a host language statement to which program execution is transferred.

Description

The NOT FOUND, SQLERROR or SQLWARNING clauses are used to look for a certain types of exception conditions resulting from the execution of an SQL statement. The NOT FOUND construction looks for a returned SQL code of +100, while SQLERROR looks for an SQL statement that has returned a negative error code. SQLWARNING looks for a return code value occurring in SQLWARN1 through SQLWARN7 of the SQLCA to determine the type of warning. For more information, see XDB Server SQL Communications Area (SQLCA).

Each occurrence of a WHENEVER statement applies to all SQL statements that follow in the source listing, until the next WHENEVER statement (of that type) is encountered. In a COBOL application, if a single WHENEVER statement is placed at the beginning of the PROCEDURE DIVISION, that statement applies to all SQL statements executed by the application program. Once the WHENEVER statement determines that an error condition has occurred, the CONTINUE option transfers program control to the next sequential statement, while the GO TO clause transfers program control to the application paragraph named in host-label.

Example:

The host-label parameter is a valid section name or unqualified paragraph name.

This assumes that error-proc is a named procedure in your COBOL program.

EXEC SQL 
    WHENEVER SQLERROR 
        GO TO error-proc
END-EXEC