Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of using "SET XACT_ABORT ON" in a stored procedure?

Tags:

sql

sql-server

What is the benefit of using SET XACT_ABORT ON in a stored procedure?

like image 533
odiseh Avatar asked Sep 25 '22 09:09

odiseh


People also ask

What is the use of set Xact_abort on?

When SET XACT_ABORT is ON, if a Transact-SQL statement raises a run-time error, the entire transaction is terminated and rolled back. When SET XACT_ABORT is OFF, in some cases only the Transact-SQL statement that raised the error is rolled back and the transaction continues processing.

What is the use of set Nocount ON OFF statement in SQL?

SET NOCOUNT ON prevents the sending of DONEINPROC messages to the client for each statement in a stored procedure.

What is set Nocount on?

SET NOCOUNT ON is a set statement which prevents the message which shows the number of rows affected by T-SQL query statements. This is used within stored procedures and triggers to avoid showing the affected rows message.

What is XACT state SQL Server?

XACT_STATE indicates whether the request has an active user transaction, and whether the transaction is capable of being committed.


1 Answers

SET XACT_ABORT ON instructs SQL Server to rollback the entire transaction and abort the batch when a run-time error occurs. It covers you in cases like a command timeout occurring on the client application rather than within SQL Server itself (which isn't covered by the default XACT_ABORT OFF setting.)

Since a query timeout will leave the transaction open, SET XACT_ABORT ON is recommended in all stored procedures with explicit transactions (unless you have a specific reason to do otherwise) as the consequences of an application performing work on a connection with an open transaction are disastrous.

There's a really great overview on Dan Guzman's Blog,

like image 274
Ben Griswold Avatar answered Oct 17 '22 10:10

Ben Griswold