使用 GOTO

GOTO 语句使 Transact-SQL 批处理的执行跳至标签。不执行 GOTO 语句和标签之间的语句。使用下列语法定义标签名:

        label_name:
      

尽量少使用 GOTO 语句。过多使用 GOTO 语句可能会使 Transact-SQL 批处理的逻辑难于理解。使用 GOTO 实现的逻辑几乎完全可以使用其他控制流语句实现。GOTO 最好用于跳出深层嵌套的控制流语句。

标签是 GOTO 的目标,它仅标识了跳转的目标。标签不隔离其前后的语句。执行标签前面语句的用户将跳过标签并执行标签后的语句。除非标签前面的语句本身是控制流语句(如 RETURN),这种情况才会发生。

以下是一个 GOTO 的示例:

IF (SELECT SYSTEM_USER()) = 'payroll'
   GOTO calculate_salary
-- Other program code would appear here.
-- When the IF statement evaluates to TRUE, the statements
-- between the GOTO and the calculate_salary label are
-- ignored. When the IF statement evaluates to FALSE the
-- statements following the GOTO are executed.
calculate_salary:
   -- Statements to calculate a salary would appear after the label.

请参阅

参考