with(nolock) or (nolock) - Is there a difference?

64,709

Solution 1

There is no functional difference, but eventually the syntax without WITH will not work. This has been deprecated:

select customer, zipcode from customers c (nolock) 

So you should be using this format:

select customer, zipcode from customers c with (nolock) 

Not using the WITH keyword for table hints has been deprecated since at least SQL Server 2008. Search the following topic for the phrase Specifying table hints without using the WITH keyword.:

http://msdn.microsoft.com/en-us/library/ms143729%28SQL.100%29.aspx

(Discussions about whether you should be using nolock at all, of course, are separate. I've blogged about them here.)

Solution 2

Though we dont find difference between (nolock) and with(nolock) ... with (nolock) would not work in SQL Server 2000 version.

And I also noticed that when you try to pull data from linked servers, just ' (nolock) ' will not work whereas you should use ' with (nolock) '.

-- this will not work 
select * from server1.DB1.dbo.table1 (nolock)

-- this will  work 
select * from server1.DB1.dbo.table1 with (nolock)

Solution 3

It really depends on which version of SQL Server you're on.

Checking out the latest documentation for SQL Server 2012 table hints omitting WITH is a deprecated feature. So while from customers c (nolock) will probably work; you should really be using from customers c WITH (nolock)

Note that this is different than from customers nolock; where nolock would serve as the table alias.

Functionally; they appear to be the same.

Solution 4

I tried this for a 170000+ data row result, however I did not see any difference through the query execution plan. Both work in the same way.

Share:
64,709
Rob
Author by

Rob

http://apus.academia.edu/RobMitchell

Updated on March 21, 2020

Comments

  • Rob
    Rob about 4 years

    Everything is based on the assumption that with(nolock) is entirely appropriate for the situtation. There are already plenty of questions out there debating whether or not to use with(nolock).

    I've looked around and haven't been able to find if there is an actual difference between using with(nolock):

    select customer, zipcode from customers c with(nolock) 
    

    or just (nolock):

    select customer, zipcode from customers c (nolock) 
    

    Is there a functional difference between the two? Stylistic?
    Is one older than the other and has a chance of being deprecated?

  • Rob
    Rob almost 11 years
    For reference on when to use with(nolock): stackoverflow.com/questions/686724/…
  • Leigh
    Leigh over 10 years
    This should be a comment, not an answer. It was already mentioned twice that there is no functional difference .. over a year ago.
  • Rob
    Rob over 10 years
    Thanks for taking the time to post. This should be a comment to Aaron's post above but I can see you don't yet have the reputation to post comments. +1 to get you on your way.
  • J. Chris Compton
    J. Chris Compton over 7 years
    It is nice to know that you've compared the plans on a table that isn't small. For future reference it would probably be helpful to know which version number of SQL Server you're using. (+1 anyway)
  • Dono
    Dono over 6 years
    @AaronBertrand The MSDN link that you specified indicates that NOLOCK is deprecated only for UPDATE and DELETE statements. It does not seem to be deprecated for SELECT statements.
  • Adir D
    Adir D over 6 years
    @Dono I never said NOLOCK was deprecated. I said NOLOCK without the WITH keyword. Note the difference between my two code samples.