DF186: GOTO statement is used.

Last modified: December 25, 2024

The topic describes the DF186 T-SQL code analysis rule.

Category

BEST PRACTICE

Message

GOTO statement is used.

Description

To improve readability, it is recommended to avoid using the GOTO statement.

Noncompliant code example

DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
  BEGIN
    SELECT @Counter AS counter
    SET @Counter = @Counter + 1
    IF @Counter = 4
      BEGIN
        GOTO Branch_One --Jumps to the first branch.
      END
    IF @Counter = 5
      BEGIN
        GOTO Branch_Two --This will never execute.
      END
  END
Branch_One:
  SELECT 'Jumping To Branch One.' AS message
  GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
  SELECT 'Jumping To Branch Two.' AS message
Branch_Three:
  SELECT 'Jumping To Branch Three.' AS message