Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

GOTO and T-SQL Expand / Collapse
Author
Message
Posted Wednesday, December 21, 2011 10:30 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 11:26 AM
Points: 561, Visits: 2,415
Comments posted to this topic are about the item GOTO and T-SQL


Best wishes,

Phil Factor
Simple Talk
Post #1225546
Posted Wednesday, December 21, 2011 11:31 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:59 AM
Points: 35,951, Visits: 30,239
Well said, Phil. Perhaps this very well written editorial could be followed by a deeper article on why people shouldn't jump the gun because they read a title or an article by a famous author. Like Sergiy taught me, "A Developer must NOT GUESS! A Developer must know." The only way to know is to do some additional research and conduct your own tests with an open mind.

I've run into similar problems. For example, I've been told by a couple of friend DBAs in the past that we don't have to concern ourselves with properly sizing a database, that SQL Server was built to automatically handle all of that right from the startup of a new server. The information came from a sentence in the front of a book written by a very well known and respected author. People just don't understand that even "experts" like well known authors can make a "mistrake".



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1225569
Posted Thursday, December 22, 2011 2:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 4:37 AM
Points: 157, Visits: 245
Well said. When I read the first part of your paragraph I was shocked to see that I am being told not to use GOTO.

I had a similar reaction when reading an article about not using DBCC SHRINKFILE, at the start it was NO NO NO. But reading the whole article tells you that is not the case. I'm sure many people must read the first bit and leave it at that.
Post #1225620
Posted Thursday, December 22, 2011 2:33 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 10:18 AM
Points: 294, Visits: 1,008
I have never felt the need to write a goto statement.
Post #1225623
Posted Thursday, December 22, 2011 2:36 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 1:27 AM
Points: 2,366, Visits: 1,837
I like this sentence in the article.

It is a shame that useful, but subtle, guidance can morph so rapidly into a ‘policy’ that becomes ‘mandatory’.


"Keep Trying"
Post #1225627
Posted Thursday, December 22, 2011 2:56 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 8:46 AM
Points: 335, Visits: 1,956
It can work the other way too, where something is promoted early in an article and then disparaged straight after. Of course, once a reader has read the for arguements, they feel that they understand the subject and decide not to read any more.

The waterfall accident: http://pascal.gugenberger.net/thoughts/waterfall-accident.html
Post #1225647
Posted Thursday, December 22, 2011 3:11 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:42 AM
Points: 2,865, Visits: 1,705
I very rarely use GOTO, in fact I cannot remember when I last used it.

If I have a large stored proc with a complex transaction then on failure of any part of the transaction I might use GOTO to jump to the ROLLBACK TRANSACTION code.

To be honest, TRY...CATCH handles most scenarios for me.

GOTO is useful when some processing logic results in a value that indicates that the rest of the transactional process should not proceed.
The alternative is to set a BIT variable to TRUE and after each step in the transaction set it to FALSE if the success criteria is not met.
Wrap each step in the transaction up in
IF @SuccessFLAG=1
BEGIN
...
END


LinkedIn Profile
Newbie on www.simple-talk.com
Post #1225655
Posted Thursday, December 22, 2011 3:21 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Today @ 4:49 AM
Points: 1,491, Visits: 976
I think GOTO is great and like to employ it at every opportunity. How else do you sensibly structure conditional exit points?

Just kidding. I don't think I've ever felt the need in my SQL or code of any sort - save perhaps some error handling in Access if I can remember that far back? Anyway I would not be scared if I did need to use it it's fair to say.
Post #1225671
Posted Thursday, December 22, 2011 3:25 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 2:21 AM
Points: 1,170, Visits: 2,153
I found myself using one just the other day, and although it was by far the neatest and most readable option for the logic I was using, I still felt the need to add an apology in the comments!

Like everything (even, dare I say it, cursors), it's got its place, but anything more than occasional use should be a pointer for closer scrutiny.
Post #1225674
Posted Thursday, December 22, 2011 4:22 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 7:46 AM
Points: 331, Visits: 879
In an age when animal could still speak...
Ok, not that long ago, about 12 years ago I got my first programming lessons I used the GOTO instead of using OO functions.
Over the years as my knowledge of programming grew and my enthusiasm to program grew less, I started to replace the GOTO less.

I didn't even know that it was in the SQL options.
In programming I can think of one use to still use it, to quickly skip code your debugging but the project can no longer wait to go live.
Post #1225713
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse