SQL Server-流程控制 1,Begin...End 语句

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

 SQL Server 流程控制中的 Begin...End 语句。

1,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

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2012-12-25 11:27  ylbtech  阅读(15588)  评论(0编辑  收藏  举报