SA0193 : Avoid unused labels to improve readability
Introduction
Section titled “Introduction”Remove unused labels in T-SQL code to improve maintainability and reduce unnecessary clutter in the codebase.
Description
Section titled “Description”In T-SQL scripts, labels can be declared for use with GOTO statements or for structured programming. However, when labels are declared but never used, they can clutter the code, making it more difficult for developers to understand and maintain the script. This practice can lead to confusion over the intended purpose of unused labels and results in less efficient code management.
For example:
-- Example of a potentially confusing and unused labelBEGIN TRANSACTION FirstLabel: -- This label is declared but not used in the code SELECT * FROM Employees;COMMIT TRANSACTIONIn the example above, FirstLabel is declared but not referenced anywhere in the script. Such labels can lead to misunderstanding and suggest functionality or control flow that doesn’t exist.
-
Unused labels can create unnecessary confusion, suggesting that there might be intentional program flow which in reality does not exist.
-
Maintenance becomes harder as developers may spend unnecessary time trying to understand the purpose of these labels.
How to fix
Section titled “How to fix”Remove unused labels from your T-SQL code to improve code clarity and maintainability.
Follow these steps to address the issue:
1.Review your T-SQL script to identify any declared but unused labels. These are typically followed by a colon ( : ).
2.Evaluate the necessity of each identified label. If a label is declared but not referenced by any GOTO statement or needed for structured programming, consider it unused.
3.Remove unused labels to simplify the code. Ensure that no GOTO statements or program logic depend on these labels before deleting them.
For example:
-- Original code with an unused labelBEGIN TRANSACTION FirstLabel: SELECT * FROM Employees;COMMIT TRANSACTION
-- Revised code with unused label removedBEGIN TRANSACTION SELECT * FROM Employees;COMMIT TRANSACTIONThe 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”2 minutes per issue.
Categories
Section titled “Categories”Design 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”DECLARE @Counter int;SET @Counter = 1;WHILE @Counter < 10BEGIN SELECT @Counter SET @Counter = @Counter + 1 IF @Counter = 4 GOTO Branch_One -- Jumps to the first branch. -- IF @Counter = 5 GOTO Branch_Two -- This will never execute.ENDBranch_One: SELECT 'Jumping To Branch One.' GOTO Branch_Three; --This will prevent Branch_Two from executing.Branch_Two: SELECT 'Jumping To Branch Two.'Branch_Three: SELECT 'Jumping To Branch Three.'Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0193 : Avoid unused labels to improve readability. | 13 | 0 |