WHENEVER

Specifies the default action after running an Embedded SQL statement when a specific condition is met.

Syntax:

>>--EXEC SQL---WHENEVER---.-NOT FOUND--.----------------->
                          +-SQLERROR---+
                          +-SQLWARNING-+ 

 >--.-CONTINUE--------.----END-EXEC---><
    +-PERFORM label---+
    +-GOTO stmt_label-+ 

Parameters:

CONTINUE Causes the next sequential statement in the source program to run.
PERFORM label Identifies a paragraph or section of code to be performed when a certain error level is detected.
GOTO stmt_label Identifies the place in the program that takes control when a certain error level is detected.

Comments:

The WHENEVER statement specifies the default action after running an Embedded SQL statement on each of the following conditions: NOT FOUND, SQLERROR, SQLWARNING.

Condition sqlcode
No error 0
NOT FOUND 100
SQLWARNING +1
SQLERROR < 0 (negative)

The scope of a WHENEVER statement is related to the position of statements in the source program, not in the run sequence. The default is CONTINUE for all three conditions.

Example:

The following example shows the WHENEVER statement in use:

     EXEC SQL WHENEVER sqlerror PERFORM errormessage1 END-EXEC

     EXEC SQL 
        DELETE FROM staff
        WHERE staff_id = 'hello' 
     END-EXEC

     EXEC SQL 
        DELETE FROM students
        WHERE student_id = 'hello'
     END-EXEC

     EXEC SQL WHENEVER sqlerror CONTINUE END-EXEC

     EXEC SQL 
        INSERT INTO staff VALUES ('hello')
     END-EXEC

     DISPLAY 'Sql Code is: ' SQLCODE
     EXEC SQL WHENEVER sqlerror PERFORM errormessage2 END-EXEC

     EXEC SQL 
        INSERT INTO staff VALUES ('hello again') 
     END-EXEC

     STOP RUN.

 errormessage1 SECTION.
     display "SQL DELETE error: " sqlcode
     EXIT.

 errormessage2 SECTION.
     display "SQL INSERT error: " sqlcode
     EXIT.