Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
dmilam
Posting Yak Master
185 Posts |
Posted - 2011-04-11 : 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
15732 Posts |
Posted - 2011-04-11 : 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
185 Posts |
Posted - 2011-04-11 : 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
15732 Posts |
Posted - 2011-04-11 : 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
185 Posts |
Posted - 2011-04-11 : 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)
7020 Posts |
Posted - 2011-04-11 : 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
185 Posts |
Posted - 2011-04-11 : 18:30:45
|
Makes sense. I'm being passed fragments, it would seem. |
|
|
|
|
|
|
|