

declare @sum smallint,@i smallint
set @i=1
set @sum=0
label:
if (@i<=100)
begin
set @sum=@sum+@i
set @i=@i+1
goto label
end
print @sum
================================================
都说不要用goto,可我看了一些经典sql 代码,也是常用goto的呀?大家如何看待goto呢?
(转自:http://topic.csdn.net/t/20060324/22/4638629.html)
---------------------------------------------------------------------------
都说不要用goto,可我看了一些经典sql 代码,也是常用goto的呀?大家如何看待goto呢?
大多用到出错时的跳转,可是想问问大家,如果不用goto,用什么办法实现这个功能呢?
---------------------------------------------------------------------------
goto不是功能不好,而是会使得代码混乱,可读性差。一般改用IF等判断语句
---------------------------------------------------------------------------
我还是觉得最好不用!!
怕用成习惯!!
一多起来!跳到什么地方去了都不知道!(个人意见!!)
---------------------------------------------------------------------------
另外一个例子:


GO
/****** 对象: StoredProcedure [dbo].[spb_WidgetInstances_UserInitialize] 脚本日期: 01/18/2010 15:55:00 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE procedure [dbo].[spb_WidgetInstances_UserInitialize]
(
@OwnerID int,
@PageCode char(4),
@Theme nvarchar(32)
)
as
SET Transaction Isolation Level Read UNCOMMITTED
SET NOCOUNT ON
if (@OwnerID<>0)
begin
Begin transaction
delete from spb_LayoutPages where OwnerID=@OwnerID and PageCode=@PageCode and Theme=@Theme
if(@@error<>0) goto Failure
delete from spb_WidgetsInZones where OwnerID=@OwnerID and ZoneID in
(select ZoneID from spb_WidgetZones where PageCode=@PageCode and Theme=@Theme)
if(@@error<>0) goto Failure
insert into spb_WidgetsInZones (WidgetID,OwnerID,ZoneID,WidgetXml,DisplayOrder)
(select W.WidgetID,@OwnerID,W.ZoneID,W.WidgetXml,W.DisplayOrder from spb_WidgetsInZones W with (nolock) inner join spb_WidgetZones Z with (nolock)
on W.ZoneID = Z.ZoneID where W.OwnerID=0 and Z.PageCode=@PageCode and Z.Theme=@Theme)
if(@@error<>0) goto Failure
commit transaction
return
Failure:
rollback transaction
return
end