SQL語句 goto

來源:互聯網
上載者:User
代碼

/********************* 求1+2+3+4+......................+100的和*******************************/
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等判斷語句

---------------------------------------------------------------------------

我還是覺得最好不用!!  
  怕用成習慣!!  
  一多起來!跳到什麼地方去了都不知道!(個人意見!!)

---------------------------------------------------------------------------

另外一個例子:

 

代碼

USE [spb2.3]
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

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.