WHENEVER is not an executable command. It can only be embedded in a host language. WHENEVER cannot be dynamically prepared.
No special type of authorization is required.
WHENEVER {NOT FOUND | SQLERROR | SQLWARNING} {CONTINUE | GO TO host-label}
host-label | Preceded by the keywords GO TO (or GOTO), specifies a host language statement to which program execution is transferred. |
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
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.