GOTO (Transact-SQL)GOTO (Transact-SQL)

適用於: 是SQL Server (從 2008 開始) 是Azure SQL Database 否Azure SQL 資料倉儲 否平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

將執行流程變更到標籤。Alters the flow of execution to a label. 略過一或多個遵照 GOTO 的 Transact-SQLTransact-SQL 陳述式,從標籤開始繼續處理。The Transact-SQLTransact-SQL statement or statements that follow GOTO are skipped and processing continues at the label. 程序、批次或陳述式區塊內的任何位置,都可以使用 GOTO 陳述式。GOTO statements and labels can be used anywhere within a procedure, batch, or statement block. GOTO 陳述式可以有巢狀結構。GOTO statements can be nested.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

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

引數Arguments

labellabel
這是 GOTO 的目標標籤,處理程序在這個點之後開始。Is the point after which processing starts if a GOTO is targeted to that label. 標籤必須遵照識別碼的規則。Labels must follow the rules for identifiers. 標籤可用來作為是使用 GOTO 的註解化方法。A label can be used as a commenting method whether GOTO is used.

RemarksRemarks

GOTO 可以在條件式流程控制陳述式、陳述式區塊或程序內,但它不能移至批次之外的標籤。GOTO can exist within conditional control-of-flow statements, statement blocks, or procedures, but it cannot go to a label outside the batch. GOTO 分支可以移至定義在 GOTO 之前或之後的標籤。GOTO branching can go to a label defined before or after GOTO.

權限Permissions

GOTO 權限預設會授與任何有效的使用者。GOTO permissions default to any valid user.

範例Examples

下列範例顯示如何使用 GOTO 做為分支機制。The following example shows how to use GOTO as a branch mechanism.

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.';  

另請參閱See Also

流程控制語言 (Transact-SQL) Control-of-Flow Language (Transact-SQL)
BEGIN...END (Transact-SQL) BEGIN...END (Transact-SQL)
BREAK (Transact-SQL) BREAK (Transact-SQL)
CONTINUE (Transact-SQL) CONTINUE (Transact-SQL)
IF...ELSE (Transact-SQL) IF...ELSE (Transact-SQL)
WAITFOR (Transact-SQL) WAITFOR (Transact-SQL)
WHILE (Transact-SQL)WHILE (Transact-SQL)