根据Books Online 的文档SET XACT_ABORT ON
,我得到的印象是,如果 T-SQL 语句引发运行时错误,则整个事务将终止并回滚:
评论
当 SET XACT_ABORT 为 ON 时,如果 Transact-SQL 语句引发运行时错误,则整个事务将终止并回滚。
在 SQL Server 2008 R2 中对此进行测试:
SET XACT_ABORT ON;
BEGIN TRANSACTION;
PRINT 'TranCount befor an error = '+CAST(@@Trancount AS varchar(50))
DROP TABLE QuertyAsdf
PRINT 'TranCount after an error = '+CAST(@@Trancount AS varchar(50))
给出输出:
TranCount befor an error = 1
Msg 3701, Level 11, State 5, Line 6
Cannot drop the table 'QwertyAsdf', because it does not exist or you do not have permission.
TranCount after an error = 1
SET XACT_ABORT ON
如果出现错误,我也认为终止批处理:
SET XACT_ABORT ON 指示 SQL Server 回滚整个事务并在发生运行时错误时中止批处理。
听起来很方便。我怎样才能让它也这样做?