SA0241 : Check transaction and savepoint names for following specified naming convention
Introduction
Section titled “Introduction”Consistent and meaningful naming of transactions and savepoints enhances readability and maintainability, reducing confusion and errors in complex or collaborative environments.
Description
Section titled “Description”Using consistent and meaningful names for transactions and savepoints improves code readability and maintainability. When names deviate from a standard pattern, it can lead to confusion and errors, especially in complex databases or collaborative projects.
For example:
-- Example of a transaction with a non-standard nameBEGIN TRAN myTrans;SAVE TRAN s1;COMMIT TRAN myTrans;In this example, the transaction name myTrans and savepoint name s1 do not follow any specific naming convention, which can make it harder to understand the purpose and scope of the transaction in larger scripts.
-
Lack of standard naming can complicate debugging and maintenance, as developers may struggle to associate transactions with their actions.
-
Non-descriptive names reduce code clarity, impacting code reviews and increasing the likelihood of errors in multi-developer environments.
How to fix
Section titled “How to fix”Ensure transaction and savepoint names adhere to specified naming conventions to improve code readability and maintainability.
Follow these steps to address the issue:
1.Review the existing transaction and savepoint names within your T-SQL scripts. Identify names that do not conform to your project’s established naming conventions.
2.Update non-standard names to follow the specified patterns outlined in the rule parameters. Ensure that names are descriptive and indicative of their purpose.
3.Test the updated T-SQL scripts to confirm that all transaction and savepoint names are correctly applied and that the refactoring has not introduced any errors.
For example:
-- Example of corrected transaction names with standard conventionsBEGIN TRAN Transaction_OrderProcess;SAVE TRAN Savepoint_BeforePayment;COMMIT TRAN Transaction_OrderProcess;The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”| Name | Description | Default Value |
|---|---|---|
| TransactionNamePattern | Transaction name pattern. | regexp:[A-Z][A-Za-z_]+ |
| SavepointNamePattern | Savepoint name pattern. | regexp:[A-Z][A-Za-z_]+ |
| DistributedTransactionNamePattern | Distributed transaction name pattern. | regexp:[A-Z][A-Za-z_]+ |
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”Naming Rules, Code Smells
Additional Information
Section titled “Additional Information”There is no additional info for this rule.
Example Test SQL
Section titled “Example Test SQL”BEGIN DISTRIBUTED TRAN TRAN1COMMIT TRAN TRAN1
BEGIN TRANSACTION TRAN_1SAVE TRANSACTION TRAN_1_SAVEPOINTROLLBACK TRAN TRAN_1
DECLARE @TranName VARCHAR(20);SELECT @TranName = 'TRAN_3a';SET @TranName = 'TRAN_3b';BEGIN TRANSACTION @TranName;COMMIT TRANSACTION @TranName;Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0241 : The distributed transaction name [TRAN1] does not match the naming convention. The expected key name is [[A-Z][A-Za-z_]+]. | 1 | 24 |
| 2 | SA0241 : The transaction name [TRAN_1] does not match the naming convention. The expected key name is [[A-Z][A-Za-z_]+]. | 4 | 18 |
| 3 | SA0241 : The savepoint name [TRAN_1_SAVEPOINT] does not match the naming convention. The expected key name is [[A-Z][A-Za-z_]+]. | 5 | 17 |
| 4 | SA0241 : The transaction name [TRAN_3b] does not match the naming convention. The expected key name is [[A-Z][A-Za-z_]+]. | 11 | 18 |