转至繁体中文版     | 网站首页 | 文章中心 | 下载中心 | 图片中心 | 笑话频道 | 教程频道 | 会员中心 | 雁过留声 | 
最新公告:     "MinTao学以致用网"欢迎您的光临,你的支持便是我们的动力,欢迎广大网友和各界人士亲临指导,你们的一个小小的建议便是我们发展的开路石!  [MinTao  2007年9月5日]        
您现在的位置: MinTao学以致用网 >> 文章中心 >> 电子课堂 >> 数据库 >> Sql Server >> 文章正文
专题栏目
更多内容
最新推荐 更多内容
相关文章
A problem when connect
求日期所属星座的 T-SQL
用于执行(计算) 字符串表
T-SQL Extractor
T-SQL: 读取磁盘文件
RS.OPEN SQL,CONN,A,B 全
T-SQL 存储过程创建 PDF
T-SQL 查询中使用的函数
T-SQL 生成 两个新的真正
浅谈SQL SERVER 2000 T-
更多内容
Building a T-SQL Loop         
Building a T-SQL Loop
作者:keminlau 文章来源:不详 点击数: 更新时间:2007-11-14 12:55:46
y_Id =2 or 3, where as there are records for Part_Id = 2 with all values for the Category_Id column. This is because the IF statement in the inner loop forced the BREAK statement to exit the inner loop. Since there were records generate for Part_Id = 2, shows that the BREAK statement only exited the inner loop and not the outer loop.

Now just to stay with the same example I''''ve been using, let''''s replace the BREAK statement in the code above with a CONTINUE statement. Here is the code for demonstrating the CONTINUE statement.

	declare @Part_Id int
	declare @Category_Id int
	declare @Desc varchar(50)
	create table PART (Part_Id int, Category_Id int, Description varchar(50))
	set @Part_Id = 0
	set @Category_Id = 0 
	while @Part_Id < 2
	begin
	  set @Part_Id = @Part_Id + 1
	  while @Category_Id < 3
	  begin
	    set @Category_Id = @Category_Id + 1 
	    If @Category_ID = 2 and @Part_ID = 1
	      Continue
	    set @Desc = ''''Part_Id is '''' + cast(@Part_Id as char(1)) +
	                '''' Category_Id '''' + cast(@Category_Id as char(1))
	    insert into PART values(@Part_Id, 
	                            @Category_Id,
	                            @Desc )
	  end  
	  set @Category_Id = 0 
	end
	select * from PART
	drop table PART

When you use the CONTINUE statement you get the following output.

	----------- ----------- ----------------------------------------- 
	1           1           Part_Id is 1 Category_Id 1
	1           3           Part_Id is 1 Category_Id 3
	2           1           Part_Id is 2 Category_Id 1
	2           2           Part_Id is 2 Category_Id 2
	2           3           Part_Id is 2 Category_Id 3

As you can see, when I use the CONTINUE statement only the record with Category_Id = 2 and Part_Id = 1 is missing. This is because the CONTINUE statement does not break out of the inner WHILE loop but only goes back to the top of the WHILE loop without inserting the record. This happens only when Category_Id is 2 and Part_Id is equal to 1. When Part_Id = 1 and Category_Id = 3 the insert statement is still executed.

GOTO Statement

The BREAK statement will only exit you from the currently processing WHILE loop, it will not break out of all WHILE loops. However, occasionally this is the kind of functionality your T-SQL script needs. To have your code break out of all WHILE loops, no matter how many nested WHILE statements you have, you will need to use the GOTO statement. Now I know most programmers cringe at the thought of using the GOTO statement, but in this case I feel the GOTO is an except able practice. Using my same example I will use the GOTO to break out of both WHILE loops, when the PART_Id = 1 and the Category_ID=3.

	declare @Part_Id int
	declare @Category_Id int
	declare @Desc varchar(50)
	create table PART (Part_Id int, Category_Id int, Description varchar(50))
	set @Part_Id = 0
	set @Category_Id = 0 
	while @Part_Id < 2
	begin
	  set @Part_Id = @Part_Id + 1
	  while @Category_Id < 3
	  begin
	    set @Category_Id = @Category_Id + 1 
	    If @Category_ID = 3 and @Part_ID = 1
	      GOTO BREAK_OUT
	    set @Desc = ''''Part_Id is '''' + cast(@Part_Id as char(1)) +
	                '''' Category_Id '''' + cast(@Category_Id as char(1))
	    insert into PART values(@Part_Id, 
	                            @Category_Id,
	                            @Desc )
	  end  
	  set @Category_Id = 0 
	end
	BREAK_OUT:
	select * from PART
	drop table PART

Here is the output from this GOTO code:

	Part_Id     Category_Id Description                                        
	----------- ----------- ----------------------------------------- 
	1           1           Part_Id is 1 Category_Id 1
	1           2           Part_Id is 1 Category_Id 2

Here the GOTO logic stopped the insertion of records into the PART table when @Category_ID = 3 and @Part_Id = 1. This is done by executing the "GOTO BREAKOUT" statement. Note that when this GOTO statement was executed it branched to the label "BREAK OUT:" which can be found following the END statement for the first, outer most WHILE statement.

Conclusion

Hopefully now you have a better idea of how to code a T-SQL WHILE loop. I''''ve explained how to control the WHILE loop, break out of a loop by using the BREAK statement, use the CONTINUE statement to skip some of the code in the while loop, and/or break out of all WHILE loops using the GOTO statement. The techniques I''''ve described should give you the basis for building all your WHILE statements from a single WHILE loop to a complex set of nested WHILE loops. My next article in this series will discuss how to process through a set of records.

上一页  [1] [2] 

文章录入:mintao    责任编辑:mintao 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007 MinTao学以致用网(www.mintao.net) Inc All Rights Reserved.
    QQ:543098146有事请Q我! QQ:261561092有事请Q我 QQ:179647303有事请Q我 MSN:min906@126.com
    站长:MinTao 信息产业部ICP备案号:鄂ICP备07500065号

    学以致用是我们学习者的至高境界和不懈追求,[MinTao学以致用网]与大家共同学习,共同进步……
    信息产业部备案
    *鄂ICP备07500065号