WHENEVER
Go Up to Statement and Function Reference (Language Reference Guide)
Traps SQLCODE errors and warnings. Available in gpre.
WHENEVER {NOT FOUND | SQLERROR | SQLWARNING}
{GOTO label | CONTINUE};
| Argument | Description |
|---|---|
|
|
Traps |
|
|
Traps |
|
|
Traps |
|
|
Jumps to program location specified by <label> when a warning or error occurs |
|
|
Ignores the warning or error and attempts to continue processing |
Description: WHENEVER traps for SQLCODE errors and warnings. Every executable SQL statement returns a SQLCODE value to indicate its success or failure. If SQLCODE is zero, statement execution is successful. A non-zero value indicates an error, warning, or not found condition.
If the appropriate condition is trapped for, WHENEVER can:
- Use
GOTOlabel to jump to an error-handling routine in an application. - Use
CONTINUEto ignore the condition.
WHENEVER can help limit the size of an application, because the application can use a single suite of routines for handling all errors and warnings.
WHENEVER statements should precede any SQL statement that can result in an error. Each condition to trap for requires a separate WHENEVER statement. If WHENEVER is omitted for a particular condition, it is not trapped.
Precede error-handling routines with WHENEVER … CONTINUE statements to prevent the possibility of infinite looping in the error-handling routines.
Example: In the following code from an embedded SQL application, three WHENEVER statements determine which label to branch to for error and warning handling:
EXEC SQL
WHENEVER SQLERROR GO TO Error; /* Trap all errors. */
EXEC SQL
WHENEVER NOT FOUND GO TO AllDone; /* Trap SQLCODE = 100 */
EXEC SQL
WHENEVER SQLWARNING CONTINUE; /* Ignore all warnings. */
For a complete discussion of error-handling methods and programming, see the Embedded SQL Guide.