GOTO (T-SQL)

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.

Syntax

Define the label:

label:

Alter the execution:

GOTO label

Arguments
label
Is the point after which processing begins if a GOTO is targeted to that label. Labels must follow the rules for identifiers. A label can be used as a commenting method whether or not GOTO is used.
Remarks

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.

Permissions

GOTO permissions default to any valid user.

Examples

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

  

See Also
BEGIN...END IF...ELSE
BREAK WAITFOR
CONTINUE WHILE
Control-of-Flow Language Using Identifiers

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.