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.
label_name
(identifier) - Required. A valid label defined in the same execution context (batch, proc, or trigger).IF…ELSE, WHILE, BREAK, CONTINUE, RETURN, TRY…CATCH
Microsoft SQL Server 6.0
GOTO immediately moves execution to a labeled line in the same batch, skipping any intermediate code.
No. GOTO is proprietary to T-SQL (SQL Server) and Sybase ASE.
No. SQL Server raises error 4112 if you attempt to jump into or out of TRY or CATCH.
Use WHILE with BREAK and CONTINUE for clearer, structured looping logic.