49

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?

johnnyRose
  • 6,834
  • 17
  • 41
  • 61
Rob
  • 805
  • 1
  • 10
  • 15
  • 1
    see here: http://stackoverflow.com/questions/1723910/syntax-for-nolock-in-sql – paul Aug 24 '12 at 16:02
  • 2
    They are alias. When the hint is specified with another option, the hint must be specified with the WITH keyword: `FROM t WITH (TABLOCK, INDEX(myindex))` http://msdn.microsoft.com/en-us/library/ms187373.aspx – edze Aug 24 '12 at 16:08
  • 1
    The Nolock can also be called as READUNCOMMITTED and it only applied inSELECT statements. It specifies that no shared locks can be issued against the table, which preventsother transactions from modifying the data in table. take a look on this post http://www.sqlserverlogexplorer.com/difference-between-nolock-and-with-nolock/ – Jason Clark Feb 23 '16 at 13:41

4 Answers4

62

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

Aaron Bertrand
  • 261,961
  • 36
  • 448
  • 471
  • 1
    For reference on when to use `with(nolock)`: http://stackoverflow.com/questions/686724/sql-when-should-you-use-with-nolock?rq=1 – Rob May 15 '13 at 20:03
  • @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. – Dono Sep 28 '17 at 02:15
  • @Dono I never said `NOLOCK` was deprecated. I said `NOLOCK` without the `WITH` keyword. Note the difference between my two code samples. – Aaron Bertrand Sep 28 '17 at 13:27
7

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)
pb2q
  • 56,563
  • 18
  • 143
  • 144
Vsh
  • 71
  • 1
  • 1
  • 1
    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. – Rob Dec 23 '13 at 17:34
6

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.

Jim B
  • 8,036
  • 10
  • 48
  • 77
2

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.

Sameer Singh
  • 1,350
  • 1
  • 20
  • 47
Avinash
  • 47
  • 2
  • 6
    This should be a comment, not an answer. It was already mentioned twice that there is no functional difference .. over a year ago. – Leigh Nov 18 '13 at 14:03
  • 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) – J. Chris Compton Jan 05 '17 at 14:54