| Author |
Topic  |
|
|
dmilam
Posting Yak Master
184 Posts |
Posted - 04/11/2011 : 12:24:45
|
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
15559 Posts |
Posted - 04/11/2011 : 13:21:59
|
| If there's no code after the Exithandler label (except for RETURN), then it's safe to replace the GOTO with RETURN. |
 |
|
|
dmilam
Posting Yak Master
184 Posts |
Posted - 04/11/2011 : 13:30:48
|
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. |
 |
|
|
robvolk
Most Valuable Yak
USA
15559 Posts |
Posted - 04/11/2011 : 13:41:20
|
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. |
 |
|
|
dmilam
Posting Yak Master
184 Posts |
Posted - 04/11/2011 : 15:06:05
|
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. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
USA
6997 Posts |
Posted - 04/11/2011 : 15:26:02
|
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 |
 |
|
|
dmilam
Posting Yak Master
184 Posts |
Posted - 04/11/2011 : 18:30:45
|
| Makes sense. I'm being passed fragments, it would seem. |
 |
|
| |
Topic  |
|