Skip to content
Advertisement

Do I need to set XACT_ABORT to ON in every trigger?

This was an old SQL Server 2008 Express database (five of them actually) that I just migrated to SQL Server 2019 Express. Everything seemed to be working fine until my crew got in and we were getting an error everywhere. Turns out we had RAISEERROR in the triggers, and even though my compatibility appears to be set to 2008 (100), we were still getting the error. So I upgraded to THROW. Now everything appears to work fine, but as I’m not a DBA, I’m worried that my upgrade my corrupt some data or leave orphans. Here’s an example of one of the triggers:

Do I need to put SET XACT_ABORT ON on every trigger (hundreds of them)? Should I? Do I still need ROLLBACK TRANSACTION after every THROW?

Before I adjusted to THROW, I was getting the “syntax error near 44447” error. the line previously looked more like this:

Thanks for the help.

Advertisement

Answer

Well really you should be solving this issue with a Foreign Key Constraint (thanks Charlieface), however if thats not practical read on…

SET XACT_ABORT ON is the default for a trigger.

OFF is the default setting in a T-SQL statement, while ON is the default setting in a trigger.

You should also be using proper joins rather than implicit joins. I have laid it out how I would write a trigger, with correctly terminated statements.

You could of course continue to use RAISERROR but it appears the syntax has changed since you originally wrote it – so you would have to correct that. Since you are having to modify them all anyway, moving to THROW seems appropriate.

I find your integrity check interesting, I would have written it as follows due to how I approach the logic, but I don’t think its any better.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement