SA0152 : THROW statement appears as a transaction name in ROLLBACK TRANSACTION

Not terminating ROLLBACK TRANSACTION with a semicolon before a THROW statement can lead SQL Server to misinterpret THROW as a transaction name.

Description

In T-SQL for SQL Server, a common issue arises when a THROW statement is unintentionally used as a transaction name in a ROLLBACK TRANSACTION statement. This can lead to errors if the ROLLBACK TRANSACTION statement is not properly terminated with a semicolon before the THROW statement.

For example:

SQL
 1-- Example of problematic code without semicolon
 2BEGIN TRY
 3    BEGIN TRANSACTION
 4    SELECT 1/0
 5    COMMIT TRANSACTION
 6END TRY
 7BEGIN CATCH
 8    IF XACT_STATE() <> 0 ROLLBACK TRANSACTION
 9    THROW
10END CATCH

This example generates a runtime error because SQL Server mistakenly tries to interpret THROW as a transaction name, resulting in a message: “Cannot roll back THROW. No transaction or savepoint of that name was found.”

  • Transaction rollback failures when the semicolon is omitted.

  • Misinterpretation of THROW as a transaction name, causing runtime errors.

How to fix

Correctly terminate ROLLBACK TRANSACTION statements with a semicolon to prevent SQL Server from interpreting the THROW keyword as a transaction name.

Follow these steps to address the issue:

  1. Locate the ROLLBACK TRANSACTION statement within your T-SQL code.

  2. Ensure the ROLLBACK TRANSACTION statement is terminated with a semicolon (;) before the THROW statement.

  3. Review the surrounding transaction logic to verify transaction consistency and correct error handling.

For example:

SQL
1BEGIN TRY
2    BEGIN TRANSACTION;
3    SELECT 1/0;
4    COMMIT TRANSACTION;
5END TRY
6BEGIN CATCH
7    IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
8    THROW;
9END CATCH

Scope

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

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
8 minutes per issue.
Categories

Design Rules, Bugs

Additional Information
Example Test SQL
SQL
 1BEGIN TRY
 2BEGIN TRANSACTION
 3SELECT 1/0
 4COMMIT TRANSACTION
 5END TRY
 6BEGIN CATCH
 7IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
 8THROW
 9END CATCH
10
11
12BEGIN TRY
13BEGIN TRANSACTION
14SELECT 1/0
15COMMIT TRANSACTION
16END TRY
17BEGIN CATCH
18IF XACT_STATE() <> 0 ROLLBACK TRANSACTION
19THROW
20END CATCH

Analysis Results
  Message Line Column
1 SA0152 : THROW statement will be considered as a transaction name due to a missing semicolon statement terminator after ROLLBACK TRANSACTION statement. 18 30
See Also

Other Resources