WHENEVER statement

Use the WHENEVER statement to trap exceptions that occur during the execution of SQL statements. The WHENEVER statement is equivalent to placing an exception-checking routine after every SQL statement.

Syntax

(1)
Notes:
  • 1 HCL OneDB™ extension
Element Description Restrictions Syntax
label Statement label to which program control transfers when an exception occurs Must exist in the same source-code module. Language-specific
routine Name of a user-defined routine (UDR) to be invoked when an exception occurs No arguments; UDR must exist at compile time. Identifier

Usage

Important: Use this statement only with .
The following table summarizes the types of exceptions that you can check with the WHENEVER statement.
Type of exception WHENEVER Keyword For More Information
Errors SQLERROR or ERROR SQLERROR Keyword
Warnings SQLWARNING Keyword
Not Found or End of Data NOT FOUND Keywords

Programs that do not use the WHENEVER statement do not automatically abort when an exception occurs. Such programs must explicitly check for exceptions and take whatever corrective action their logic specifies. If you do not check for exceptions, the program simply continues running. If errors occur, however, the program might not perform its intended purpose.

The first keyword that follows WHENEVER specifies some type of exceptional condition; the last part of the statement specifies some action to take when the exception is encountered (or no action, if CONTINUE is specified). The following table summarizes possible actions that WHENEVER can specify.
Type of action WHENEVER keyword For more information
Continue program execution CONTINUE Keyword
Stop program execution STOP Keyword
Transfer control to a specified label GOTO GO TO GOTO Keyword
Transfer control to a UDR CALL Clause