Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


GOTO and T-SQL


GOTO and T-SQL

Author
Message
Phil Factor
Phil Factor
Right there with Babe
Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)

Group: General Forum Members
Points: 741 Visits: 2937
Comments posted to this topic are about the item GOTO and T-SQL


Best wishes,

Phil Factor
Simple Talk
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44981 Visits: 39869
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Riken Patel
Riken Patel
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 Visits: 373
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.
IceDread
IceDread
Old Hand
Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)

Group: General Forum Members
Points: 329 Visits: 1145
I have never felt the need to write a goto statement.
ChiragNS
ChiragNS
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2409 Visits: 1865
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"
Samuel Vella
Samuel Vella
Old Hand
Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)

Group: General Forum Members
Points: 393 Visits: 2140
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
David.Poole
David.Poole
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3667 Visits: 3112
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
call.copse
call.copse
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2843 Visits: 1857
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.
Andrew Watson-478275
Andrew Watson-478275
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1657 Visits: 2653
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.
Resender
Resender
SSC-Addicted
SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)

Group: General Forum Members
Points: 473 Visits: 1564
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search