Specifies whether SQL server automatically rolls back to the current transaction when a running error occurs in a Transact-SQL statement.
Transact-SQL syntax conventions
Syntax
|
Set xact_abort {on | off} |
Remarks
When set xact_abort is on, if a running error occurs when a Transact-SQL statement is executed, the entire transaction is terminated and rolled back.
When set xact_abort is off, only the wrong Transact-SQL statement is generated and the transaction will continue to be processed. If the error is serious, the entire transaction may be rolled back even if set xact_abort is off.
Compilation errors (such as syntax errors) are not affected by set xact_abort.
For most Ole databasesProgram(Including SQL Server), xact_abort In the implicit or display transaction data modification statement must be set to on. The only case where this option is not required is when the Provider supports nested transactions. For more information, see .
Set xact_abort is set during execution or running, rather than during analysis.
Example
BelowCodeIn this example, a foreign key conflict error occurs in transactions that contain other Transact-SQL statements. Errors are generated in the first statement set, but other statements are successfully executed and the transaction is successfully committed. In the second statement set, SetSet xact_abort
SetOn
. This causes a statement error to terminate the batch processing and roll back the transaction.
|
copy Code |
Use adventureworks; 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 ()); 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 |
See