Skip to content

SA0193 : Avoid unused labels to improve readability

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

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 label
BEGIN TRANSACTION
FirstLabel: -- This label is declared but not used in the code
SELECT * FROM Employees;
COMMIT 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.

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 label
BEGIN TRANSACTION
FirstLabel:
SELECT * FROM Employees;
COMMIT TRANSACTION
-- Revised code with unused label removed
BEGIN TRANSACTION
SELECT * FROM Employees;
COMMIT TRANSACTION

The rule has a Batch scope and is applied only on the SQL script.

Rule has no parameters.

The rule does not need Analysis Context or SQL Connection.

2 minutes per issue.

Design Rules, Code Smells

There is no additional info for this rule.

DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN
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.
END
Branch_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.'
  Message Line Column
1 SA0193 : Avoid unused labels to improve readability. 13 0

Analysis Rules