本文共 1735 字,大约阅读时间需要 5 分钟。
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 go10 --=============================================================11 -- 2,Syntax12 --=============================================================13 While Boolean_expression14 {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 go25 --=============================================================26 -- 3,Example27 -- Desc:输出产品编号为10以内的产品名称。28 --=============================================================29 use Northwind30 go31 32 Declare @id int33 Declare @productName varchar(40)34 35 Set @id=136 37 While @id<1038 Begin39 select @productName=ProductName from Products where ProductID=@id40 Print @productName41 Set @id=@id+142 End43 44 go45 --=============================================================46 -- 4,Operation result47 --=============================================================48 --Chai49 --Chang50 --Aniseed Syrup51 --Chef Anton's Cajun Seasoning52 --Chef Anton's Gumbo Mix53 --Grandma's Boysenberry Spread54 --Uncle Bob's Organic Dried Pears55 --Northwoods Cranberry Sauce56 --Mishi Kobe Niku
本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2012/12/25/2832092.html,如需转载请自行联系原作者