Code
/*********************
Calculate the sum of 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
========================================================== ==========
Do not use goto, but I have read some classic SQL code, which is also common for goto? What do you think about goto?
(From: http://topic.csdn.net/t/20060324/22/4638629.html)
---------------------------------------------------------------------------
Do not use goto, but I have read some classic SQL code, which is also common for goto? What do you think about goto?
Most of them use redirection when an error occurs. However, if you do not need goto, how can you implement this function?
---------------------------------------------------------------------------
Goto is not a poor function, but can make the code messy and have poor readability. Generally, use the IF and other judgment statements.
---------------------------------------------------------------------------
I still think it is best not to use it !!
Afraid to use it as a habit !!
More! I don't know where to jump! (Personal opinion !!)
---------------------------------------------------------------------------
Another example:
Code
USE [spb2.3]
GO
/***** Object: StoredProcedure [dbo]. [spb_WidgetInstances_UserInitialize] script Date: 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