SQL Server 的 SET XACT_ABORT ON | OFF;
2013-08-23 17:48
239 查看
一、当 SET XACT_ABORT ON; 时,一个事务(BEGIN TRANSACTION; ......(SQL语句)......COMMIT TRANSACTION;)中,如果有报错,就回滚整个事务。
二、当 SET XACT_ABORT ON; 的根据XACT_STATE()的状态判断是否回滚事务。
USE AdventureWorks2008R2; GO IF OBJECT_ID(N't2', N'U') IS NOT NULL DROP TABLE t2; GO IF OBJECT_ID(N't1', N'U') IS NOT NULL DROP TABLE t1; GO CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY); CREATE TABLE t2 (a INT NOT NULL REFERENCES t1(a)); GO INSERT INTO t1 VALUES (1); INSERT INTO t1 VALUES (3); INSERT INTO t1 VALUES (4); INSERT INTO t1 VALUES (6); GO SET XACT_ABORT OFF; GO BEGIN TRANSACTION; INSERT INTO t2 VALUES (1); INSERT INTO t2 VALUES (2); -- Foreign key error. INSERT INTO t2 VALUES (3); COMMIT TRANSACTION; GO SET XACT_ABORT ON; GO BEGIN TRANSACTION; INSERT INTO t2 VALUES (4); INSERT INTO t2 VALUES (5); -- Foreign key error. INSERT INTO t2 VALUES (6); COMMIT TRANSACTION; GO -- SELECT shows only keys 1 and 3 added. -- Key 2 insert failed and was rolled back, but -- XACT_ABORT was OFF and rest of transaction -- succeeded. -- Key 5 insert error with XACT_ABORT ON caused -- all of the second transaction to roll back. SELECT * FROM t2; GO
二、当 SET XACT_ABORT ON; 的根据XACT_STATE()的状态判断是否回滚事务。
USE AdventureWorks2008R2; GO -- SET XACT_ABORT ON will render the transaction uncommittable -- when the constraint violation occurs. SET XACT_ABORT ON; BEGIN TRY BEGIN TRANSACTION; -- A FOREIGN KEY constraint exists on this table. This -- statement will generate a constraint violation error. DELETE FROM Production.Product WHERE ProductID = 980; -- If the delete operation succeeds, commit the transaction. The CATCH -- block will not execute. COMMIT TRANSACTION; END TRY BEGIN CATCH -- Test XACT_STATE for 0, 1, or -1. -- If 1, the transaction is committable. -- If -1, the transaction is uncommittable and should -- be rolled back. -- XACT_STATE = 0 means there is no transaction and -- a commit or rollback operation would generate an error. -- Test whether the transaction is uncommittable. IF (XACT_STATE()) = -1 BEGIN PRINT 'The transaction is in an uncommittable state.' + ' Rolling back transaction.' ROLLBACK TRANSACTION; END; -- Test whether the transaction is active and valid. IF (XACT_STATE()) = 1 BEGIN PRINT 'The transaction is committable.' + ' Committing transaction.' COMMIT TRANSACTION; END; END CATCH; GO
相关文章推荐
- SET XACT_ABORT { ON | OFF }SQL Server 是否自动回滚到当前事务
- SET XACT_ABORT { ON | OFF }SQL Server 是否自动回滚到当前事务
- SET XACT_ABORT { ON | OFF }
- SET XACT_ABORT { ON | OFF } 设置整个事务终止并回滚方法
- SET XACT_ABORT ON [SQL SERVER] 设置事务全部回滚
- Transaction Save Point (SET XACT_ABORT { ON | OFF })
- sql set xact_abort on 用例
- SQL存储过程中的几个常见设定SET QUOTED_IDENTIFIER/NOCOUNT/XACT_ABORT ON/OFF
- 在存储过程中SET XACT_ABORT ON 有什么用
- SQL Server 中的SET XACT_ABORT各种用法及显示结果
- 存储过程中的 SET XACT_ABORT ON 和事务
- SQL_SET XACT_ABORT ON(事务回滚)
- INSERT 失败,因为下列 SET 选项的设置不正确: 'ARITHABORT' SET ARITHABORT ON | OFF
- SET XACT_ABORT ON 数据库事务
- Sql Server 使用 SET NOCOUNT { ON | OFF}
- SET XACT_ABORT on
- SQL SERVER存储过程是否返回受影响的行,SET NOCOUNT ON|OFF
- set XACT_ABORT ON与判断事务的异同
- set xact_abort ON 你懂的, 在分布式数据库事务中,用到. 在事务中,若出现错误,系统即默认回滚事务
- SQLServer 存储过程 SET NOCOUNT { ON | OFF } 的使用