?
Solved

T-SQL On Error

Posted on 2011-09-28
3
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 40

Accepted Solution

by:
lcohan earned 2000 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

770 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