SA0193 : Avoid unused labels to improve readability

Remove unused labels in T-SQL code to improve maintainability and reduce unnecessary clutter in the codebase.

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:

SQL
1-- Example of a potentially confusing and unused label
2BEGIN TRANSACTION
3    FirstLabel: -- This label is declared but not used in the code
4    SELECT * FROM Employees;
5COMMIT TRANSACTION

In 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

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:

SQL
 1-- Original code with an unused label
 2BEGIN TRANSACTION
 3    FirstLabel: 
 4    SELECT * FROM Employees;
 5COMMIT TRANSACTION
 6
 7-- Revised code with unused label removed
 8BEGIN TRANSACTION
 9    SELECT * FROM Employees;
10COMMIT TRANSACTION

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
2 minutes per issue.
Categories

Design Rules, Code Smells

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1DECLARE @Counter int;
 2SET @Counter = 1;
 3WHILE @Counter < 10
 4BEGIN 
 5    SELECT @Counter
 6    SET @Counter = @Counter + 1
 7    IF @Counter = 4 GOTO Branch_One -- Jumps to the first branch.
 8 --   IF @Counter = 5 GOTO Branch_Two  -- This will never execute.
 9END
10Branch_One:
11    SELECT 'Jumping To Branch One.'
12    GOTO Branch_Three; --This will prevent Branch_Two from executing.
13Branch_Two:
14    SELECT 'Jumping To Branch Two.'
15Branch_Three:
16    SELECT 'Jumping To Branch Three.'

Analysis Results
  Message Line Column
1 SA0193 : Avoid unused labels to improve readability. 13 0
See Also

Other Resources