SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 GOTO ExitHandler
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dmilam
Posting Yak Master

185 Posts

Posted - 04/11/2011 :  12:24:45  Show Profile  Reply with Quote
Is there a better method than using


GOTO ExitHandler;


I've come across this in some production code and it seems like RETURN would be better, but I'm not familiar enough with (historical) GOTO usage in T-SQL.

robvolk
Most Valuable Yak

USA
15688 Posts

Posted - 04/11/2011 :  13:21:59  Show Profile  Visit robvolk's Homepage  Reply with Quote
If there's no code after the Exithandler label (except for RETURN), then it's safe to replace the GOTO with RETURN.
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 04/11/2011 :  13:30:48  Show Profile  Reply with Quote
quote:
Originally posted by robvolk

If there's no code after the Exithandler label (except for RETURN), then it's safe to replace the GOTO with RETURN.



Thanks, Rob. Here's a sample:


TRUNCATE TABLE [db].[dbo].[table];
SELECT @ReturnError = @@ERROR;
IF(@ReturnError <> 0) 
BEGIN 
SET @ReturnError = 1;
SET @ReturnMessage='Error, While attempting to truncate the table [db].[dbo].[table]';
GOTO ExitHandler;   
END


After this, an INSERT into the above truncated table.
Go to Top of Page

robvolk
Most Valuable Yak

USA
15688 Posts

Posted - 04/11/2011 :  13:41:20  Show Profile  Visit robvolk's Homepage  Reply with Quote
That's not the part I was talking about, what comes after the ExitHandler label? Look for "ExitHandler:" in the code.

And as far as error handling goes, you should look at TRY...CATCH. The current code will not handle an error of severity 16 or higher (e.g. truncate a table that doesn't exist), but TRY...CATCH would.
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 04/11/2011 :  15:06:05  Show Profile  Reply with Quote
quote:
Originally posted by robvolk

That's not the part I was talking about, what comes after the ExitHandler label? Look for "ExitHandler:" in the code.



"ExitHandler:" doesn't exist in this code. There's no comment as to where or how this code fits into a larger proc, for example.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 04/11/2011 :  15:26:02  Show Profile  Reply with Quote
quote:
Originally posted by dmilam

quote:
Originally posted by robvolk

That's not the part I was talking about, what comes after the ExitHandler label? Look for "ExitHandler:" in the code.



"ExitHandler:" doesn't exist in this code. There's no comment as to where or how this code fits into a larger proc, for example.


If ExitHandler: does not exit, this statement would cause a compile time error:
GOTO ExitHandler;






CODO ERGO SUM
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 04/11/2011 :  18:30:45  Show Profile  Reply with Quote
Makes sense. I'm being passed fragments, it would seem.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000