Search code examples
javadb2db2-luw

Is there a Db2 scalar function to retrieve full error message information for a given SQLCODE?


I'd like to know if there is any existing solution for getting detailed DB error explanations in code if I have a SQL error code. I'd like to dump this info into log so that I won't have to look up google everytime I meet one.

Target tach stack is - Java, DB2.

Thank you!


Solution

  • The function SYSPROC.SQLERRM will return the error message for a given SQLCODE. https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0022027.html

    E.g. This will return the short message for SQLCODE 100 (i.e SQL100W)

    VALUES (SYSPROC.SQLERRM (100))
    

    like this

    1
    ---------------------------------------------------------------------------------------------------
    SQL0100W  No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table.
    
      1 record(s) selected.
    

    and this will return the full message

    VALUES (SYSPROC.SQLERRM ('SQL100W', '', '', 'en_US', 0))
    

    will return

    1                                                                                                                                                                                                                  
    ----------------------------------------------------------------------------
    
    SQL0100W  No row was found for FETCH, UPDATE or DELETE; or the result of
          a query is an empty table.
    
    Explanation: 
    
    One of the following conditions is true:
    
    *  No row was found that meets the search conditions specified in an
       UPDATE or DELETE statement.
    
    *  The result of a SELECT statement was an empty table.
    
    *  A FETCH statement was executed when the cursor was positioned after
       the last row of the result table.
    
    *  The result of the SELECT used in an INSERT statement is empty.
    
    No data was retrieved, updated, or deleted.
    
    User response: 
    
    No action is required. Processing can continue.
    
    sqlcode: +100
    
    sqlstate: 02000
    
    
    
      1 record(s) selected.