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


Is TRY-CATCH in SQL still a best practice?


Is TRY-CATCH in SQL still a best practice?

Author
Message
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45127 Visits: 39923
Larry Johnson-473989 (10/15/2013)
(Part of the reason I'm obsessing over this is because he is pretty full of himself and also made the statement, "Trust me, I've been doing this for 10 years," to support his argument. That kind of statement drives me nuts.)


Lordy! I agree! I hate such arrogant ring-knocking even if they happen to be qualified to make such a brag. People truly worth their salt won't make such a brag as a replacement for proof and will embrace the opportunity to mentor someone about the "why" instead of that "trust me" line of hooie. Sounds more like he was trying to impress the opposite sex than do anything DBA-like.

If I have to work with someone like this, I'll normally try to thoughtfully and kindly coach them into not saying such things and into being a more informed and bettor mentor. I do have an extremely low tolerance for such malarky though. It's a "porkchop-able" offense.

--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 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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45127 Visits: 39923
Larry Johnson-473989 (10/15/2013)
I overheard our DBA today (2013-10-17)...


Heh... unless you happen to be a master with DBCC TIMEWARP, that's gotta be a phat phinger. :-P

--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 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
lnardozi 61862
lnardozi 61862
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 615
TRY/CATCH statements are used to rollback a transaction if an error occurs in processing. The COMMIT goes at the end of the TRY and the ROLLBACK goes inside the CATCH. Without them, the transaction is left open if an error occurs during processing. This open transaction will continue to hold all the locks it established until the the connection is disconnected, the transaction is rolled back manually, or the process becomes a deadlock victim. In the case of a long running process that doesn't have a command timeout, that's essentially forever - at least until the 'all knowing' DBA terminates 'mysterious hung processes' as part of his daily routine.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47263 Visits: 44392
lnardozi 61862 (10/15/2013)
Without them, the transaction is left open if an error occurs during processing.


Unless XACT_ABORT is on.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4255 Visits: 6431
Sean Lange (10/15/2013)
If you got your quote correct that person is moron.


Well now Sean, there was no reason to hold back because we're all friends here. Why don't you tell us what you really think? :-P

I 100% agree with this and what Jeff said above also.

If I were to offer a pet-peeve, using TRY-CATCH needs to be done properly. That is, checking for XACT_STATE() and then rolling back the TRANSACTION in a properly prescribed fashion. It is particularly useful in SQL Agent run SPs that need to report when things go awry because there's no front end to handle the resulting failure.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
Jeff Moden (10/15/2013)
Sean Lange (10/15/2013)
If you got your quote correct that person is moron.


Heh... if the quote is correct, then that "DBA" is more off than on, so stop insulting morons. He's clearly a moroff. :-D


BWAHAAAA!!!!! That is awesome Jeff.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
dwain.c (10/15/2013)
Sean Lange (10/15/2013)
If you got your quote correct that person is moron.


Well now Sean, there was no reason to hold back because we're all friends here. Why don't you tell us what you really think? :-P


We are certainly all friends here. I have never had much problem telling one of my friends they are a moron when it is the correct label for the situation. :-P I would probably not normally be so direct but I just dealt with this exact same line of "reasoning" in the real world.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Kurt W. Zimmerman
Kurt W. Zimmerman
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1062 Visits: 1396
Here is a clear example where bad coding practices continue to propagate. It's MOROFFs like this that give a bad name to DBAs....

Kurt

Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45127 Visits: 39923
lnardozi 61862 (10/15/2013)
TRY/CATCH statements are used to rollback a transaction if an error occurs in processing. The COMMIT goes at the end of the TRY and the ROLLBACK goes inside the CATCH. Without them, the transaction is left open if an error occurs during processing. This open transaction will continue to hold all the locks it established until the the connection is disconnected, the transaction is rolled back manually, or the process becomes a deadlock victim. In the case of a long running process that doesn't have a command timeout, that's essentially forever - at least until the 'all knowing' DBA terminates 'mysterious hung processes' as part of his daily routine.


Like I said in one of my previous posts on this thread and like what Gail just confirmed, that would all be true except for when SET XACT_ABORT is ON. Read about what it does and how it works in "Books Online".

--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 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
webskater
webskater
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 479
GilaMonster (10/15/2013)
lnardozi 61862 (10/15/2013)
Without them, the transaction is left open if an error occurs during processing.


Unless XACT_ABORT is on.


Can I just check. In the pseudo code below, assume Col1 is an int


DECLARE @ReturnValue int

BEGIN TRANSACTION

INSERT INTO MyTable(Col1)
VALUES (25)
IF @@ERROR <> 0
BEGIN
SET @ReturnValue = -999
ROLLBACK TRANSACTION
RETURN @ReturnValue
END

INSERT INTO MyTable(Col1)
VALUES (17)
IF @@ERROR = 0
BEGIN
SET @ReturnValue = -998
ROLLBACK TRANSACTION
RETURN @ReturnValue
END

COMMIT TRANSACTION



Are you saying that unless XACT_ABORT is ON - in the example above, the first insert will not be rolled back?

I have just put the code above into a stored procedure and executed it. -998 is returned. No records are written into MyTable. XACT_ABORT is off.
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