DIFFERENCE BETWEEN NOLOCK VS WITH(NOLOCK) IN SQL SERVER 2008

  • Sean Lange (1/13/2014)


    I will ignore your comment about suggesting that always using NOLOCK is a good idea when integrity of the results is not important. :w00t:

    I'm sure he meant that WITH(NOLOCK) should always be used instead of NOLOCK only when the hint is mandatory and not when integrity of the results is not important.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • I will ignore your comment about suggesting that always using NOLOCK is a good idea when integrity of the results is not important.:w00t:

    To be clear (and to avoid being the guy who suggested someone use always use NOLOCK in any situation) I do want to point out that I included the caveat, "If NOLOCK is required..." I think my point would have been clearer if I said: If there is a requirement to use NOLOCK then use the always use WITH. That said, I can't think of a good reason to use NOLOCK and aver that a requirement that anyone must use NOLOCK is ridiculous. 😉

    Edit: Just Louis' response; that is what I meant... Thank you sir.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • Alan.B (1/13/2014)


    I will ignore your comment about suggesting that always using NOLOCK is a good idea when integrity of the results is not important.:w00t:

    To be clear (and to avoid being the guy who suggested someone use always use NOLOCK in any situation) I do want to point out that I included the caveat, "If NOLOCK is required..." I think my point would have been clearer if I said: If there is a requirement to use NOLOCK then use the always use WITH. That said, I can't think of a good reason to use NOLOCK and aver that a requirement that anyone must use NOLOCK is ridiculous. 😉

    Edit: Just Louis' response; that is what I meant... Thank you sir.

    Yes 10000% agreed. I must have misread your original post and/or was just being a pissy boy at that moment. My comment was unintentionally (and hopefully uncharacteristically) rude. My apologies if I offended you, I certainly did not mean any.

    _______________________________________________________________

    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/

  • GilaMonster (1/9/2014)


    The first is deprecated, the second is not, both can result in incorrect results, duplicate rows and missing rows, so neither is the best to use. Best is to write the SQL properly without hints.

    Hi Gila,

    What if we applied READ_COMMITTED_SNAPSHOT ISOLATION LEVEL on the database level? would that be enough to return only committed & not dirty data?

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • I would suggest (if NOLOCK is a requirement) to use NOLOCK, not WITH (NOLOCK).

    NOLOCK without brackets is not a hint, but a table name alias.

    So, the query would be using appropriate transaction isolation and no dirty reads would be involved.

    :hehe:

    _____________
    Code for TallyGenerator

  • Sergiy (11/4/2015)


    I would suggest (if NOLOCK is a requirement) to use NOLOCK, not WITH (NOLOCK).

    NOLOCK without brackets is not a hint, but a table name alias.

    So, the query would be using appropriate transaction isolation and no dirty reads would be involved.

    :hehe:

    BWAAAA-HAAAA! (Sorry... did that come out loud?) 😛

    --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)

  • Sergiy (11/4/2015)


    I would suggest (if NOLOCK is a requirement) to use NOLOCK, not WITH (NOLOCK).

    NOLOCK without brackets is not a hint, but a table name alias.

    So, the query would be using appropriate transaction isolation and no dirty reads would be involved.

    :hehe:

    Haha. Brilliant!

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

Viewing 7 posts - 16 through 21 (of 21 total)

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