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

The topic describes the SA0152 analysis rule.

Message

THROW statement appears as a transaction name in ROLLBACK TRANSACTION

Description

The rule checks for THROW statement appearing as a transaction name in ROLLBACK TRANSACTION statement.

The THROW statement is not a reserved keyword and is acceptable as a transaction name identifier.

This may lead to a problem in a scenario when a ROLLBACK TRANSACTION statement is a followed by a THROW statement.

If the ROLLBACK TRANSACTION statement is not followed by the semicolon (;) statement terminator, the THROW statement will be recognized as a transaction_name parameter provided to the ROLLBACK TRANSACTION statement.

This example code works as expected:

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

This code generates a runtime error: Cannot roll back THROW. No transaction or savepoint of that name was found.

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

How to fix

Review the code and add the missing semicolon after the TRANSACTION keyword.

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