7. OpenSQL Features : Error Handling : Error Trapping Using Whenever Statement
 
Share this page                  
Error Trapping Using Whenever Statement
The WHENEVER statement specifies a particular action to be performed whenever a particular condition is true. Since conditions are set to true by values in the SQLCA sqlcode, the WHENEVER statement responds only to errors generated by embedded OpenSQL database statements. Forms statements do not set sqlcode.
The conditions that indicate errors or warnings are as follows:
sqlwarning
Indicates that the executed OpenSQL database statement produced a warning condition. Sqlwarning becomes true when the SQLCA sqlwarn0 variable is set to “W.”
sqlerror
Indicates that an error occurred in the execution of the database statement. Sqlerror becomes true when the SQLCA sqlcode variable contains a negative number.
There are two other conditions that are more closely related to status conditions rather than error conditions. For a complete list of the conditions, see Whenever in the chapter “OpenSQL Statements.”
The actions that can be specified for these conditions are listed in the following table:
Action
Description
CONTINUE
Execution continues with the next statement.
STOP
Prints an error message and terminates the program's execution. Pending updates are not committed.
GOTO label
Performs a host language “go to.”
CALL procedure
Calls the specified host language procedure. If call sqlprint is specified, the sqlprint procedure prints the error or warning message and continues with the next statement.
In an application program, a WHENEVER statement is in effect until the next WHENEVER statement (or the end of the program). For example, if you put the following statement in your program:
exec sql whenever sqlerror call myhandler;
OpenSQL traps errors for all database statements in your program that physically follow the WHENEVER statement to the procedure, myhandler. A WHENEVER statement does not affect the statements that physically precede it.
If your program includes an SQLCA, OpenSQL will not display error messages unless your application issues a WHENEVER ... SQLPRINT statement, or you set II_EMBED_SET to sqlprint.