在 sql server 2008 中,我使用这样的模式:
begin transaction
begin try
/* do something */
end try
begin catch
if @@TRANCOUNT > 0
rollback
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity,1)
end catch
if @@TRANCOUNT > 0
commit transaction
当我在 Sql Server Management Studio 上点击“取消执行查询”按钮时,它会取消查询并使事务保持打开状态。
这是预期的行为吗?或者我的模式有错误。难道不应该回滚事务吗?
恕我直言,这是一种有意的行为。当您取消正在运行的查询时(如果存在打开的事务),它将保持打开状态,直到您显式提交或回滚它OR直到连接未关闭
您的模式中没有任何有价值的错误。如果您手动控制执行流程(取消执行查询),那么您应该以相同的方式手动处理打开的事务。
Update:
The behavior is controlled by SSMS option Disconnect after the query executes - which means that the query disconnects after execution or cancel and rolls back opened transactions:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)