JDBC SQL Server raised error handling in multi statement stored procedures

前端 未结 1 989
情深已故
情深已故 2021-01-24 14:43

I have a multi-statement stored procedure that first performs a select and then raises an error if certain conditions are met.

The raise error in the stored procedure do

相关标签:
1条回答
  • 2021-01-24 15:03

    The way the SQL server protocol works, you first need to process the result set produced by the select, and then move to the next result to get the exception.

    To process all results (result sets, update counts and exceptions), you need do something like:

    CallableStatement csmt = ...;
    boolean isResultSet = cstmt.execute();
    do {
       if (isResultSet) {
           // process result set
           try (ResultSet rs = csmst.getResultSet()) {
               while(rs.next()) {
                   // ...
               }
           }
       } else {
           int updateCount = rs.getUpdateCount();
           if (updateCount == -1) {
               // -1 when isResultSet == false means: No more results
               break;
           } else {
               // Do something with update count
           }
       }
       isResultSet = cstmt.getMoreResults();
    } while(true);
    

    When the execution of the stored procedure reaches the exception, this will also report the exception to your java application (iirc from getMoreResults()).

    0 讨论(0)
提交回复
热议问题