Solved

T-SQL On Error

Posted on 2011-09-28
3
202 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Update trigger 5 17
Query Syntax 17 34
T-SQL: "HAVING CASE" Clause 1 24
insert wont work in SQL 14 21
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 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