Solved

T-SQL On Error

Posted on 2011-09-28
3
200 Views
Last Modified: 2012-05-12
I have a store procedure that does the following:

do some task
IF @Error <> 0 GOTO LOOPERROR

do some task
IF @Error <> 0 GOTO ExitPoint

Can this also be done with RETURN?  I tried the following but i got an error

do some task
IF @Error <> 0 Return ExitPoint

0
Comment
Question by:yanci1179
3 Comments
 
LVL 21

Expert Comment

by:JestersGrind
ID: 36719426
Maybe, GOTO jumps to a defined label.  Return exits the procedure.  If you need to run some additional code when an error occurs, you would use GOTO to jump to that code.  Otherwise if you just want to exit the stored procedure, return would work, but you don't need the label "ExitPoint".

Greg

0
 
LVL 5

Expert Comment

by:MrNetic
ID: 36813545
You can declare one more variable, and set the value fot that variable on the ExitPoint you are, and after the goto labet add return @variable
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 36893661
I think the logic above is not quite ok because in both cases IF @Error <> 0 you go either to LOOPERROR and/or RETURN (exit)

do some task
IF @Error <> 0 GOTO LOOPERROR

do some task
IF @Error <> 0 GOTO ExitPoint


In my opinion the code should be:

do some taskA
IF @Error <> 0
    GOTO LOOPERROR

do some taskB
IF @Error <> 0
    GOTO LOOPERROR
ELSE
     IF @Error =0
            RETURN
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now