ylbtech-SQL Server:SQL Server-流程控制 1,Begin...End 语句 |
SQL Server 流程控制中的 Begin...End 语句。

1 --=============================================================
2 -- 1, Begin...End语句
3 -- Desc:Begin...End通常用来表示一个语句块,其内部的代码可以包含一组T-SQL语句
4 -- ,凡是在这个语句块里的所有代码,都属于同一流程控制,其语法代码如下。
5 -- author:ylbtech
6 -- pubdate:10:39 2012/12/15
7 --=============================================================
8 go
9
10 go
11 --=============================================================
12 -- 2,Syntax
13 --=============================================================
14 Begin
15 {
16 sql_statement|statement_block
17 }
18 End
19 --Remark:其中,sql_statement参数和statement_block参数为任何有效的T-SQL语句或者语句组。
20 -- Begin...End语句通常与If、While语句搭配使用。
21
22 go
23 --=============================================================
24 -- 3,Example
25 -- Desc:查看商品表中名称为“Gorgonzola Telino”的产品单价是否低于20元,如果低于20元的话
26 -- ,查看其订购量。其代码如下。
27 --=============================================================
28 --select * from Products where ProductName=N'Gorgonzola Telino'
29 use Northwind
30 go
31 Declare @price money
32 Declare @productId int
33 Declare @count int
34
35 select @price=UnitPrice,@productId=ProductID from Products where ProductName='Gorgonzola Telino'
36
37 if @price<$20
38 Begin
39 Print 'UnitPrice under $20'
40 select @count=SUM([Order Details].Quantity) from [Orders] join [Order Details]
41 on[Orders].OrderID=[Order Details].OrderID
42 where [Order Details].ProductID=@productId
43 Print 'Quantity is:'+Cast(@count as varchar(5))
44 End
45
46 go
47 --=============================================================
48 -- 4,Operation result
49 --=============================================================
50 --UnitPrice under $20
51 --Quantity is:1397

本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2012/12/25/2832033.html,如需转载请自行联系原作者