T-SQL:流程控制 3,While 语句

ylbtech-SQL Server:SQL Server-流程控制 3,While 语句

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

1,While 语句
 1 --=============================================================

 2 -- 1, While语句

 3 -- Desc:While语句是个循环语句

 4 -- author:ylbtech

 5 -- pubdate:10:39 2012/12/15

 6 --=============================================================

 7 go

 8 

 9 go

10 --=============================================================

11 -- 2,Syntax

12 --=============================================================

13 While Boolean_expression

14     {sql_statement|statement_block}

15 [Break]

16     {sql_statement|statement_block}

17 [Continue]

18     {sql_statement|statement_block}

19     

20 --Remark:该语法解释为:当Boolean_expression为真时,循环执行While语句块代码,直到Boolean_expression为假为止。

21 --    如果要在中途中止循环的话,可以使用Break或Continue语句。Break语句是跳出目前所执行的循环,Continue语句是

22 --    终止执行代码,跳回到While的判断语句重新进行条件判断,再根据判断结果是否进入循环体。

23 

24 go

25 --=============================================================

26 -- 3,Example

27 -- Desc:输出产品编号为10以内的产品名称。

28 --=============================================================

29 use Northwind

30 go

31 

32 Declare @id int

33 Declare @productName varchar(40)

34 

35 Set @id=1

36 

37 While @id<10

38 Begin

39 select @productName=ProductName from Products where ProductID=@id

40 Print @productName

41 Set @id=@id+1

42 End

43 

44 go

45 --=============================================================

46 -- 4,Operation result

47 --=============================================================

48 --Chai

49 --Chang

50 --Aniseed Syrup

51 --Chef Anton's Cajun Seasoning

52 --Chef Anton's Gumbo Mix

53 --Grandma's Boysenberry Spread

54 --Uncle Bob's Organic Dried Pears

55 --Northwoods Cranberry Sauce

56 --Mishi Kobe Niku

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

你可能感兴趣的:(while)