Error message retrieval in embedded SQL applications

The method that you use to retrieve error information depends on the language that you used to write the application.
  • C, C++, and COBOL applications can use the GET ERROR MESSAGE API to obtain the corresponding information related to the SQLCA passed in.
    C Example: The SqlInfoPrint procedure from UTILAPI.C 
    /******************************************************************************
    ** 1.1 - SqlInfoPrint - prints diagnostic information to the screen.
    **
    ******************************************************************************/
    int SqlInfoPrint( char * appMsg,
    		  struct sqlca * pSqlca,
    		  int line,
    		  char * file )
    {   int  rc = 0;
        char sqlInfo[1024];
        char sqlInfoToken[1024];
        char sqlstateMsg[1024];
        char errorMsg[1024];
        if (pSqlca->sqlcode != 0 && pSqlca->sqlcode != 100)
        {  strcpy(sqlInfo, "");
           if( pSqlca->sqlcode < 0)
           {   sprintf( sqlInfoToken, "\n---- error report ----\n");
               strcat( sqlInfo, sqlInfoToken);
           }
           else
           {   sprintf( sqlInfoToken, "\n---- warning report ----\n");
               strcat( sqlInfo, sqlInfoToken);
           } /* endif */
    
           sprintf( sqlInfoToken, "  app. message      = %s\n", appMsg);
           strcat( sqlInfo, sqlInfoToken);
           sprintf( sqlInfoToken, "  line              = %d\n", line);
           strcat( sqlInfo, sqlInfoToken);
           sprintf( sqlInfoToken, "  file              = %s\n", file);
           strcat( sqlInfo, sqlInfoToken);
           sprintf( sqlInfoToken, "  SQLCODE           = %ld\n",
           pSqlca->sqlcode);
           strcat( sqlInfo, sqlInfoToken);
           
           /* get error message */
           rc = sqlaintp( errorMsg, 1024, 80, pSqlca);
           /* return code is the length of the errorMsg string */
           if( rc > 0)
           { sprintf( sqlInfoToken, "%s\n", errorMsg);
             strcat( sqlInfo, sqlInfoToken);
           }
           
           /* get SQLSTATE message */
           rc = sqlogstt( sqlstateMsg, 1024, 80, pSqlca->sqlstate);
           if (rc == 0)
           { sprintf( sqlInfoToken, "%s\n", sqlstateMsg);
             strcat( sqlInfo, sqlInfoToken);
           }
           
           if( pSqlca->sqlcode < 0)
           { sprintf( sqlInfoToken, "--- end error report ---\n");
             strcat( sqlInfo, sqlInfoToken);
    
             printf("%s", sqlInfo);
             return 1;
           }
           else
           { sprintf( sqlInfoToken, "--- end warning report ---\n");
             strcat( sqlInfo, sqlInfoToken);
           
             printf("%s", sqlInfo);
             return 0;
           } /* endif */
        } /* endif */
        return 0;
    }
    C developers can also use an equivalent function, sqlglm(), which has the signature:
    sqlglm(char *message_buffer_ptr, int *buffer_size_ptr, int *msg_size_ptr)
    COBOL Example: From CHECKERR.CBL
        ********************************
        * GET ERROR MESSAGE API called *
        ********************************
          call "sqlgintp" using
               by value buffer-size
               by value line-width
               by reference sqlca
               by reference error-buffer
            returning error-rc.
        ************************
        * GET SQLSTATE MESSAGE *
        ************************
          call "sqlggstt" using
               by value buffer-size
               by value line-width
               by reference sqlstate
               by reference state-buffer
            returning state-rc.
        if error-rc is greater than 0
          display error-buffer.
        
        if state-rc is greater than 0
          display state-buffer.
        
        if state-rc is less than 0
          display "return code from GET SQLSTATE =" state-rc.
        
        if SQLCODE is less than 0
          display "--- end error report ---"
          go to End-Prog.
        
        display "--- end error report ---"
        display "CONTINUING PROGRAM WITH WARNINGS!".
  • REXX applications use the CHECKERR procedure.
    /******   CHECKERR - Check SQLCODE *****/
    CHECKERR:
      arg errloc
    
      if  ( SQLCA.SQLCODE = 0 ) then
        return 0
      else do
        say '--- error report ---'
        say 'ERROR occurred :' errloc
        say 'SQLCODE :' SQLCA.SQLCODE
    
       /*********************\
       * GET ERROR MESSAGE  *
       \*********************/
        call SQLDBS 'GET MESSAGE INTO :errmsg LINEWIDTH 80'
        say errmsg
        say '--- end error report ---'
    
        if (SQLCA.SQLCODE < 0 ) then
          exit
        else do
          say 'WARNING - CONTINUING PROGRAM WITH ERRORS'
          return 0
        end
      end
    return 0