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 ««12

Is TRY-CATCH in SQL still a best practice? Expand / Collapse
Author
Message
Posted Tuesday, October 15, 2013 6:58 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 11:27 AM
Points: 36,778, Visits: 31,234
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."

(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 #1505039
Posted Tuesday, October 15, 2013 7:01 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 11:27 AM
Points: 36,778, Visits: 31,234
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.


--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."

(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 #1505040
Posted Tuesday, October 15, 2013 7:45 PM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 1:16 PM
Points: 107, Visits: 511
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.
Post #1505046
Posted Tuesday, October 15, 2013 9:04 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 9:05 AM
Points: 42,469, Visits: 35,540
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 2008, MVP
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

Post #1505051
Posted Tuesday, October 15, 2013 9:07 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:12 PM
Points: 3,616, Visits: 5,230
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?

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!
Post #1505054
Posted Wednesday, October 16, 2013 7:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:32 AM
Points: 13,121, Visits: 11,955
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.


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)
Post #1505175
Posted Wednesday, October 16, 2013 7:20 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:32 AM
Points: 13,121, Visits: 11,955
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?


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. 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)
Post #1505179
Posted Wednesday, October 16, 2013 7:39 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 8:52 AM
Points: 984, Visits: 1,324
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
Post #1505198
Posted Wednesday, October 16, 2013 7:49 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 11:27 AM
Points: 36,778, Visits: 31,234
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."

(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 #1505205
Posted Wednesday, October 16, 2013 11:14 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, May 29, 2014 5:38 PM
Points: 183, Visits: 392
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.
Post #1505346
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse