SQL Server流程控制 5,Goto 语句

ylbtech-SQL Server:SQL Server-流程控制 5,Goto 语句

 SQL Server 流程控制中的 Goto 语句。

1,Goto 语句
 1 --=============================================================
 2 -- 1, Goto语句
 3 -- Desc:Goto语句可以让程序跳转到一个指定的标签处并执行其后的代码。Goto语句和标签可以在程序
 4 -- 、批处理和语句块中的任意位置使用,也可以嵌套使用。
 5 -- author:ylbtech
 6 -- pubdate:23:32 2012/12/15
 7 --=============================================================
 8 go
 9 
10 go
11 --=============================================================
12 -- 2,Syntax
13 --=============================================================
14 lable:
15 Goto lable
16 
17 --Remark:
18 
19 go
20 --=============================================================
21 -- 3,Example
22 -- Desc:查看产品表中名称为“Gorgonzola Telino”的商品单价是否低于20元,将结果输入来。其代码如下。
23 --=============================================================
24 use Northwind
25 go
26 
27 Declare @price money
28 
29 select @price=UnitPrice from Products where ProductName='Gorgonzola Telino'
30 
31 If @price<$20
32 Goto print1
33 Else
34 Goto print2
35 
36 print1:
37 Print 'UnitPrice less than $20'
38 Goto theEnd
39 print2:
40 Print 'UnitPrice greater than $20'
41 Goto theEnd
42 theEnd:
43 
44 --Remark:label标签可以在Goto语句之前或之后,并没有限制。Goto语句也可以从多个循环中直接跳出
45 --,而Break语句只可以跳出一个While循环。
46 go
47 --使用Goto语句有几点要注意的事项:
48 --·使用Goto语句会使程序的可读性变差,能不使用时建议不要使用。
49 --·Goto语句只能从While循环或If判断的内部往外跳,不能从外部往内部跳。
50 --·Goto语句只能在当前批中跳转,不能跳转到其他批中。
51 go
52 --=============================================================
53 -- 4,Operation result
54 --=============================================================
55 --UnitPrice less than $20
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/ylbtech/p/2832112.html