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
![]() |
作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 |