Writing routines to handle warnings and errors

A good strategy for handling errors and warnings in an Embedded SQL application is to write custom procedures to handle them, then install the procedures with the whenever...perform statement.

The following example shows sample warning and error handling routines. For simplicity, both routines omit certain conditions that should normally be included: warn_para omits the code for SQLWARN1, and err_para omits the code that handles Client-Library errors and operating system errors:

* Declare the sqlca.  *
 exec sql include sqlca end-exec
 exec sql whenever sqlerror call "ERR-PARA"
     end-exec
 exec sql whenever sqlwarning call
     "WARN-PARA" end-exec
 exec sql whenever not found continue end-exec
 
 WARN-PARA.
 *    Displays error codes and numbers from the sqlca
 *    and exits with an ERREXIT status.
    DISPLAY "Warning code is " SQLCODE.
     DISPLAY "Warning message is " SQLERRMC.
    IF SQLWARN2 EQUAL “W”
         DISPLAY "Data has been truncated.".
     IF SQLWARN3 EQUAL “W”
         DISPLAY "A null value was eliminated from
 -        "    the argument set of a function.".
     IF SQLWARN4 EQUAL “W”
         DISPLAY "An into clause had too many or too
 -        "    few host variables.".
     IF SQLWARN5 EQUAL “W”
         DISPLAY "A dynamic update or delete was
 -        "    lacking a where clause.".
     IF SQLWARN6 EQUAL “W”
         DISPLAY "A server conversion or truncation
 -        "    error occurred.".
 WARN-PARA-END.
     EXIT.
 
 ERR-PARA.
 * Print the error code, the error message, and the
 * line number of the command that caused the
 * error.

    DISPLAY "Error code is " SQLCODE.
     DISPLAY "Error message is " SQLERRMC.
     STOP RUN.