SQL statement goto

Source: Internet
Author: User
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

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.