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
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()
).