Solved

T-SQL On Error

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS On fail action 5 38
SQL Server How-To Show Notes In First Row of Results 4 30
SQL Group By Question 4 19
RESTORE MASTER DATABASE -- NOW 2 19
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

856 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