Is TRY-CATCH in SQL still a best practice?

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply