Skip to content

SA0130 : Explicit error handling for statements between BEGIN TRAN and COMMIT/ROLLBACK TRAN is required

Handling of transaction errors in T-SQL is crucial to maintain database integrity and consistency.

This problem occurs when T-SQL scripts include BEGIN TRAN and COMMIT/ROLLBACK TRAN statements without adequately handling potential errors. In SQL Server, if an error occurs during a transaction and it is not properly managed, it could lead to incomplete transactions and data corruption.

For example:

BEGIN TRAN
-- Some SQL operations
UPDATE TableName SET ColumnName = 'Value' WHERE Condition;
COMMIT TRAN

This query lacks error handling. If the UPDATE operation fails, the absence of a mechanism to ROLLBACK can leave the database in an inconsistent state.

  • Unmanaged errors can lead to partial data updates, violating data integrity.

  • Failing to handle errors adequately complicates debugging and error tracking in your applications.

To ensure transaction errors are properly managed and avoid data corruption, follow these recommended steps to enhance your SQL scripts.

Follow these steps to address the issue:

1.Encapsulate all statements between the BEGIN TRAN and COMMIT/ROLLBACK TRAN statements using a TRY..CATCH block.

2.Set XACT_ABORT ON before the BEGIN TRAN statement to ensure that if a runtime error occurs, the entire transaction is automatically rolled back.

3.If the transaction spans multiple batches, check @@TRANCOUNT is greater than 0 before executing each statement. This ensures there is no data modification outside the completed transaction.

For example:

-- Ensure XACT_ABORT is ON
SET XACT_ABORT ON;
GO
BEGIN TRY
BEGIN TRAN
GO
IF @@TRANCOUNT > 0 INSERT INTO Table_5 (testkey) SELECT 2;
GO
IF @@TRANCOUNT > 0 INSERT INTO Table_5 (testkey) SELECT 3;
GO
IF @@TRANCOUNT > 0 INSERT INTO Table_5 (testkey) SELECT 4;
GO
IF @@TRANCOUNT > 0
COMMIT TRAN;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRAN;
-- Log error or raise error using THROW or RAISERROR
END CATCH

The rule has a Batch scope and is applied only on the SQL script.

NameDescriptionDefault Value
ConsiderXactAbortSettingWhen the XACT_ABORT setting is set to ON before the checked statements, the statements are ignored.yes
ReportFirstStatementOnlyIf set to ‘yes’, a warning message for only the first DML statement in the transaction scope will be generated.yes

The rule does not need Analysis Context or SQL Connection.

1 hour per issue.

Design Rules, Bugs

There is no additional info for this rule.

CREATE TABLE [dbo].[Table_5] ( [testkey] [int] NOT NULL PRIMARY KEY)
-- this example inserts 2 rows even though there is an error
-- in the transaction because of the primary key.
begin tran
insert into Table_5 (testkey) select 2
insert into Table_5 (testkey) select 2
insert into Table_5 (testkey) select 3
if @@trancount = 1
commit tran
-- This example does not insert any rows because of
-- the SET XACT_ABORT ON
SET XACT_ABORT ON
begin tran
insert into Table_5 (testkey) select 2
insert into Table_5 (testkey) select 2
insert into Table_5 (testkey) select 3
if @@trancount = 1
commit tran
SET XACT_ABORT OFF
-- This example does not insert any rows because of
-- the explicit rollback in the catch block.
begin try
begin tran
insert into Table_5 (testkey) select 2
insert into Table_5 (testkey) select 2
insert into Table_5 (testkey) select 3
commit tran
end try
begin catch
rollback tran
RAISERROR('error', 1, 1)
end catch
if @@trancount = 1
commit tran
-- This example will not insert any rows because of
-- the the transaction is rolled back
begin tran
if @@trancount >0 insert into Table_5 (testkey) select 2
if @@trancount >0 insert into Table_5 (testkey) select 2
if @@trancount >0 insert into Table_5 (testkey) select 3
if @@trancount >0
commit tran
 MessageLineColumn
1SA0130 : Explicit error handling for statements between BEGIN TRAN and COMMIT/ROLLBACK TRAN is required.70

Analysis Rules