问题描述
我有一个多语句存储过程,该过程首先执行选择,然后在满足某些条件时引发错误.
I have a multi-statement stored procedure that first performs a select and then raises an error if certain conditions are met.
但是,存储过程中的raise错误不会导致JDBC SQLException异常,正如我所期望的那样.
The raise error in the stored procedure doesn't cause a JDBC SQLException like I expect however.
如果我删除了SELECT,那么它将正常工作. 打印语句会发生相同类型的行为.
If I remove the SELECT, then it works fine. The same type of behavior occurs with the print statement.
我有多种其他方法可以解决此问题,但为了将来参考,我想知道是否存在一种方法来检查是否确实存在引发的错误.
I have multiple other ways to handle this, but for future reference I was wondering if there was a way to check if raised errors do exist.
推荐答案
SQL Server协议的工作方式,首先需要处理select生成的结果集,然后移至下一个结果以获取异常.
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);
当存储过程的执行到达异常时,这还将把异常报告给Java应用程序(来自getMoreResults()
的iirc).
When the execution of the stored procedure reaches the exception, this will also report the exception to your java application (iirc from getMoreResults()
).
这篇关于JDBC SQL Server在多语句存储过程中提出了错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!