Home Forums SQL Server 2008 T-SQL (SS2K8) DIFFERENCE BETWEEN NOLOCK VS WITH(NOLOCK) IN SQL SERVER 2008 RE: DIFFERENCE BETWEEN NOLOCK VS WITH(NOLOCK) IN SQL SERVER 2008

  • Alan.B (1/13/2014)


    kbhanu15 (1/9/2014)


    Hi All,

    can any one clarify the difference between NOLOCK and WITH(NOLOCK) in sql server 2008 ?

    which one is the best practice to use ?

    Thanks

    Bhanu

    I think everyone did a good job explaining why you would want to avoid using read-uncommitted (AKA NOLOCK). That said, if NOLOCK is required (and getting the correct results when you run a query is not required) then I recommend always using: WITH (NOLOCK). Some things break when you omit the WITH keyword when using NOLOCK.

    For example: this query would fail:

    SELECT t1.<column>, t2.<column>

    FROM <db>.<schema>.<table> t1

    JOIN <linked server>.<db>.<schema>.<table> t2 (NOLOCK)

    This would not fail:

    SELECT t1.<column>, t2.<column>

    FROM <db>.<schema>.<table> t1

    JOIN <linked server>.<db>.<schema>.<table> t2 WITH (NOLOCK)

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

    The reason omitting the keyword WITH when defining a query hint is because leaving it off has been deprecated. You should ALWAYS use proper syntax in any t-sql statement and omitting WITH for a query hint is not correct syntax.

    http://technet.microsoft.com/en-us/library/ms143729.aspx

    To see the line in reference just search for "Specifying table hints without using the WITH keyword."

    _______________________________________________________________

    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/