Alters the flow of execution to a label. The Transact-SQL statement(s) following GOTO are skipped and processing continues at the label. GOTO statements and labels can be used anywhere within a procedure, batch, or statement block. GOTO statements can be nested.
Define the label:
label:
Alter the execution:
GOTO label
GOTO can exist within conditional control-of-flow statements, statement blocks, or procedures, but it cannot go to a label outside of the batch. GOTO branching can go to a label defined before or after GOTO.
GOTO permissions default to any valid user.
This example shows GOTO looping as an alternative to using WHILE.
Note The tnames_cursor cursor is not defined. This example is for illustration only.
USE pubs
GO
DECLARE @tablename sysname
SET @tablename = N'authors'
table_loop:
IF (@@FETCH_STATUS <> -2)
BEGIN
SELECT @tablename = RTRIM(UPPER(@tablename))
EXEC ("SELECT """ + @tablename + """ = COUNT(*) FROM "
+ @tablename )
PRINT " "
END
FETCH NEXT FROM tnames_cursor INTO @tablename
IF (@@FETCH_STATUS <> -1) GOTO table_loop
GO
BEGIN...END | IF...ELSE |
BREAK | WAITFOR |
CONTINUE | WHILE |
Control-of-Flow Language | Using Identifiers |