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...call statement.

The following example shows sample warning and error handling routines. For simplicity, both routines omit certain conditions that should normally be included. warning_hndl omits the code for sqlwarn[1]. error_hndl omits the code that handles Client-Library errors and operating system errors:

/* Declare the sqlca.  */
 exec sql include sqlca;
 exec sql whenever sqlerror call error_handler();
 exec sql whenever sqlwarning call     warning_handler();
 exec sql whenever not found continue;
 /*
 ** void error_handler()
 **
 ** Displays error codes and numbers from the sqlca
 */
 void error_handler()
 {
     fprint(stderr, 
     “\n**sqlcode=(%d)”,sqlca.sqlcode);
 
 /*
 ** void warning_handler()
 **
 **  Displays warning messages.
 */
 void warning_handler()
 {
 
     if (sqlca. sqlwarn[1] == ‘W’)
     {
         fprintf(stderr, “\n** Data truncated.\n”);
     }
 
     if (sqlca.sqlwarn[3] == ‘W’)
     {
         fprintf(stderr, “\n** Insufficient
             host variables to store results.\n”);
     }
     return;
 }