本文件已封存並已停止維護。
GOTO (Transact-SQL)
SQL Server 2005
將執行流程變更到標籤。略過一或多個遵照 GOTO 的 Transact-SQL 陳述式,從標籤開始繼續處理。程序、批次或陳述式區塊內的任何位置,都可以使用 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.'
顯示:

