A COBOL-DB2 program can have multiple DB2 SQL statements. In order to implement the common error-trapping for all the SQL statements in a COBOL-DB2 program, we will use WHENEVER statement.
The WHENEVER statement can direct the control to the error handling section or routine based on the value returned in SQLCODE field of SQLCA. For example, we can declare WHENEVER statement in COBOL-DB2 program as below−
EXEC SQL WHENEVER SQLERROR GOTO ERROR-ROUTINE END-EXEC
In the above example, the SQLERROR parameter will be set only when the value of SQLCODE will be lesser than zero, which indicates that there was some error occurred while executing the query. In that case, the control will be transferred to the ERRORROUTINE paragraph of the program.