SQLServer帮助中:Transact-SQL 中使用 TRY...CATCH 说明: 编译错误和语句级重新编译错误 TRY...CATCH 将不处理
什么是:编译错误和语句级重新编译错误?例如:
begin tran
begin try
update TableName
set ....
from TableName
where ......
end try
begin catch
if (@@TranCount > 0)
rollback tran
set @ErrMessage = ERROR_MESSAGE()
return(1)
end catch
if (@@TranCount > 0)
commit tran
TableName 不存在 就是编译错误和语句级重新编译错误。
同时:RETURN_VALUE 将返回 0
如果你像我这样使用控制RETURN_VALUE 值来判定stored procedure 是否正确的执行完了,那么你将发现try catch 好像是失效了,但返回值又是说明stored procedure 正确的执行完了。
另外严重的问题是事务不会被提交导致数据库被锁住。