Hi,
I’ve been going through the below model question for MCSA (Microsoft Certified Solutions Associate) 70-461 exam. This question is frequently asked in all the exams. Let’s discuss this so that it will be helpful for those who preparing for the certification.
Question:
You created a stored procedure that will update multiple tables within a transaction. You need to ensure that if the stored procedure raises a run-time error, the entire transaction is terminated and rolled back. Which Transact-SQL statement should you include at the beginning of the stored procedure?
Options:
A. SET XACT_ABORT ON
B. SET ARITHABORT ON
C. TRY
D. BEGIN
E. SET ARITHABORT OFF
F. SET XACT_ABORT OFF
Answer: A (SET XACT_ABORT ON)
When XACT_ABORT is ON, if a transaction raises a run-time error, the entire transaction is terminated and rolled back. Let’s test the same from the following demo.
create table Test
(
sales int
)
set xact_abort on
go
begin tran
insert into test values (100/2)
insert into test values (100/0) -- Divided by zero error
insert into test values (100/4)
commit tran
select * from test
There is a “Divided by zero” error occurred in the second insert. As the XACT_ABORT is set as ON, all the transactions are terminated and rolled back.
When XACT_ABORT is set as OFF, only the statement raised the run-time error is rolled back and the transaction continues processing. XACT_ABORT OFF is the default setting. Lets test the same from following demo.
set xact_abort off
go
begin tran
insert into test values (100/2)
insert into test values (100/0) -- Divided by zero error
insert into test values (100/4)
commit tran
select * from test
However there is a “Divided by zero” error occurred in the second insert, the transaction is continues processing and it is not rolled back the entire transaction.
Thanks,
Selvaraj M.