SA0152 : THROW statement appears as a transaction name in ROLLBACK TRANSACTION
Introduction
Section titled “Introduction”Not terminating ROLLBACK TRANSACTION with a semicolon before a THROW statement can lead SQL Server to misinterpret THROW as a transaction name.
Description
Section titled “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:
-- Example of problematic code without semicolonBEGIN TRY BEGIN TRANSACTION SELECT 1/0 COMMIT TRANSACTIONEND TRYBEGIN CATCH IF XACT_STATE() <> 0 ROLLBACK TRANSACTION THROWEND CATCHThis 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
THROWas a transaction name, causing runtime errors.
How to fix
Section titled “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:
BEGIN TRY BEGIN TRANSACTION; SELECT 1/0; COMMIT TRANSACTION;END TRYBEGIN CATCH IF XACT_STATE() <> 0 ROLLBACK TRANSACTION; THROW;END CATCHThe rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”8 minutes per issue.
Categories
Section titled “Categories”Design Rules, Bugs
Additional Information
Section titled “Additional Information”Example Test SQL
Section titled “Example Test SQL”BEGIN TRYBEGIN TRANSACTIONSELECT 1/0COMMIT TRANSACTIONEND TRYBEGIN CATCHIF XACT_STATE() <> 0 ROLLBACK TRANSACTION;THROWEND CATCH
BEGIN TRYBEGIN TRANSACTIONSELECT 1/0COMMIT TRANSACTIONEND TRYBEGIN CATCHIF XACT_STATE() <> 0 ROLLBACK TRANSACTIONTHROWEND CATCHAnalysis Results
Section titled “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 |