SQL Keywords

SQL GOTO

What is SQL GOTO?

Transfers program control to a user-defined label within the same batch, stored procedure, or trigger.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL GOTO: SQL Server (all supported versions) and Sybase ASE. Not available in PostgreSQL, MySQL, Oracle, SQLite, or ANSI Standard SQL.

SQL GOTO Full Explanation

GOTO is a Transact-SQL (T-SQL) control-of-flow statement that immediately redirects execution to a labeled statement defined in the same batch, stored procedure, or trigger. Labels are identifiers followed by a colon placed on their own line. Because GOTO performs an unconditional jump, any statements between the GOTO call and the target label are skipped. GOTO cannot cross scope boundaries such as TRY/END TRY, CATCH/END CATCH blocks, or OUTER APPLY/INNER APPLY statements, and it cannot jump into or out of a batch, function, or dynamic SQL block executed with EXEC. While fully supported in SQL Server, GOTO is considered a last-resort flow-control mechanism; IF…ELSE, WHILE, or structured error handling are preferred for readability and maintainability. Excessive or careless use can lead to “spaghetti code,” making logic hard to trace and debug.

SQL GOTO Syntax

-- Define a label
label_name:
    statement ;

-- Jump to the label
GOTO label_name;

SQL GOTO Parameters

  • label_name (identifier) - Required. A valid label defined in the same execution context (batch, proc, or trigger).

Example Queries Using SQL GOTO

-- Example 1: Skip a block when a condition is true
DECLARE @flag BIT = 1;
IF @flag = 1
    GOTO SkipBlock;

PRINT 'This line is skipped when @flag = 1';

SkipBlock:
PRINT 'Flow continued after label';

-- Example 2: Simple loop using GOTO
DECLARE @i INT = 1;
LoopStart:
IF @i > 5 GOTO LoopEnd;
PRINT CONCAT('Row ', @i);
SET @i += 1;
GOTO LoopStart;
LoopEnd:
PRINT 'Loop finished';

Expected Output Using SQL GOTO

  • Execution jumps directly to the specified label, omitting any statements in between
  • In Example 2, numbers 1-5 are printed followed by "Loop finished"

Use Cases with SQL GOTO

  • Bypassing large blocks of code based on a quick precondition check
  • Exiting deeply nested logic without refactoring existing code
  • Implementing simple loops in legacy scripts where WHILE is not desired
  • Providing a single exit point for complex validation routines

Common Mistakes with SQL GOTO

  • Forgetting the colon after a label definition
  • Using GOTO across TRY…CATCH boundaries (raises error 4112)
  • Jumping into the middle of statement blocks, leading to undefined state
  • Creating infinite loops by omitting a terminating condition

Related Topics

IF…ELSE, WHILE, BREAK, CONTINUE, RETURN, TRY…CATCH

First Introduced In

Microsoft SQL Server 6.0

Frequently Asked Questions

What does the SQL GOTO statement do?

GOTO immediately moves execution to a labeled line in the same batch, skipping any intermediate code.

Is GOTO ANSI standard SQL?

No. GOTO is proprietary to T-SQL (SQL Server) and Sybase ASE.

Can GOTO cross TRY...CATCH blocks?

No. SQL Server raises error 4112 if you attempt to jump into or out of TRY or CATCH.

What is a better alternative to GOTO for loops?

Use WHILE with BREAK and CONTINUE for clearer, structured looping logic.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!