GOTO (Transact-SQL)

将执行流更改到标签处。跳过 GOTO 后面的 Transact-SQL 语句,并从标签位置继续处理。GOTO 语句和标签可在过程、批处理或语句块中的任何位置使用。GOTO 语句可嵌套使用。

主题链接图标Transact-SQL 语法约定

语法

 Define the label:  label :  Alter the execution: GOTO label 

参数

  • label
    如果 GOTO 语句指向该标签,则其为处理的起点。标签必须符合标识符规则。无论是否使用 GOTO 语句,标签均可作为注释方法使用。

备注

GOTO 可出现在条件控制流语句、语句块或过程中,但它不能跳转到该批以外的标签。GOTO 分支可跳转到定义在 GOTO 之前或之后的标签。

权限

GOTO 语句的权限默认情况下授予任何有效用户。

示例

以下示例显示如何将 GOTO 用作分支机制。

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

请参阅

参考

控制流语言 (Transact-SQL)
BEGIN...END (Transact-SQL)
BREAK (Transact-SQL)
CONTINUE (Transact-SQL)
IF...ELSE (Transact-SQL)
WAITFOR (Transact-SQL)
WHILE (Transact-SQL)

其他资源

使用 GOTO
使用标识符作为对象名称

帮助和信息

获取 SQL Server 2005 帮助