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 |
sql_2k
Starting Member
26 Posts |
Posted - 2008-02-28 : 07:02:22
|
Please can any one explain the scenarios where the GO TO can be advisable to use. Also why GO TO is banned to be used. |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-02-28 : 07:20:33
|
I can't think of any situation in which I would advise using GOTO.There are many more visually correct structured statements than GOTO.GOTO is bad because it gets messy as the code base expands. Basically it becomes what is called as "spaghetti code" where you can't make head or tail of it. Simple GOTO is not that bad, but any real life solution hardly remains simple over a period of time and believe me, entangled mess of GOTO statements in a big 2000 lines of SP is a nightmare to maintain!Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-28 : 07:45:17
|
It is mostly used in Front end application. In sql you can use it in place of while loopexdeclare @i intset @i=1test:print @iset @i=@i+1if @i<=100 goto testMadhivananFailing to plan is Planning to fail |
 |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-02-28 : 08:58:52
|
i never use it. the only (possibly) reasonable use for it I can think of is if you are religious about having a single point of exit for a routine, like in this pseudocode:do stuffif error goto ErrorExitdo other stuffif error goto ErrorExit:Exitprint 'succeeded'return:ErrorExitprint 'failed'EDIT: fixed typos elsasoft.org |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-02-28 : 09:16:58
|
GOTO is just another tool in the bag. GOTO does not create "2000 line spaghetti code", bad developers create "2000 line spaghetti code".I don't use STUFF very often either but every once in a while it is the right tool for the job.Be One with the OptimizerTG |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-02-28 : 09:28:59
|
quote: Originally posted by jezemine i never use it. the only (possibly) reasonable use for it I can think of is if you are religious about having a single point of exit for a routine, like in this pseudocode:do stuffif error goto ErrorExitdo other stuffif error goto ErrorExit:Exitprint 'succeeded'return:ErrorExitprint 'failed'EDIT: fixed typos elsasoft.org
I use it like this because I code a standard error exit routine in stored procedures that is too many lines of code to duplicate after each error check.I also use it sometimes to conditionally bypass or exit sections of code.Start_section_001:If condition goto Start_section_001_Exit:…do stuff...If condition2 goto Start_section_001_Exit:…do more stuff...Start_section_001_Exit: CODO ERGO SUM |
 |
|
sql_2k
Starting Member
26 Posts |
Posted - 2008-02-28 : 09:48:36
|
Thanks all for your reply and advise. I have used GOTO for handling error in couple of my SPs and I think as suggested by Michael we can use the GOTO in such scenarios. Though GOTO is not a stuctured approach but definitely it's not bad if not used excessively.Regards,Rain.quote: Originally posted by TG GOTO is just another tool in the bag. GOTO does not create "2000 line spaghetti code", bad developers create "2000 line spaghetti code".I don't use STUFF very often either but every once in a while it is the right tool for the job.Be One with the OptimizerTG
|
 |
|
|
|
|