top of page

About XACT_ABORT()

What makes this difficult is that most errors do not stop processing , so when you have a group of modification statements running in a batch without any error handling, they keep running.

XACT_ABORT is a SET options that ends the batch on a transaction. The way this works is that when running, if an erroroccurs, the batch stops and the transaction is stopped. It is effective, but gives you no realcontrol over what happens in an error. Since the batch ends immediately, to know whatstatement you are executing you need to print messages constantly. Hence this method ismore used for system tasks, but it is an effective tool for dealing with rolling back atransaction on an error.

The SET XACT_ABORT option is set to OFF by default, therefore you must enable the option when you want to ensure that SQL Server rolls back a failed transaction.

Here is an example,

What's wrong with this?

Here we should add SET XACT_ABORT() ON after SET NOCOUNT ON. As we discussed earlier, SET XACT_ABORT() ON changes statement terminating errors into batch-terminating errors. It will cause the transaction to roll back after the error in the UPDATE statement.


aboutME

I am John Fan Zhang, a data analyst and finance researcher. I hold a PhD in finance, CFA charter and full membership of CFA New Zealand Society. I have fifteen-year experience in corporate investment and eight-year experience in advanced data analysis. My research focuses on the effect of social psychology (culture) on financial decisions. Finance research involves heaps of data analyses that lead me to the data field. I am a Microsoft Certified Solutions Expert (MCSE): Data Management and Analytics (Excel, Power BI, and SQL). Aside from Excel, Power BI and SQL, I am also familiar with econometric tools such as Stata, Eviews, and MATLAB. I use OX and Python for programming. I am an active data community event participant, volunteer, speaker, moderator, program reviewer, including PASS Marathon 2020, Global AI BootCamp Auckland 2019, SQL Saturday Auckland (2017, 2018, 2019), and Definity Conference (2018, 2019, 2020, Auckland, New Zealand).

Auckland, New Zealand

  • Google Site
  • Twitter
  • LinkedIn

©2016 BY JOHN'S DATA STORY

bottom of page