Solved

T-SQL On Error

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Task 11 44
Applying Roles in Common Scenarios 3 19
SQL Server code help needed 14 35
Need SQL Update 2 9
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

730 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