Questions tagged [nolock]

A SQL Server table hint that can be used to prevent queries from issuing shared read locks. While often used to prevent a query from blocking other queries it also makes it susceptible to dirty reads and other potential data issues.

From the SQL Server Books Online topic on Table Hints:

Is equivalent to READUNCOMMITTED.


Specifies that dirty reads are allowed. No shared locks are issued to prevent other transactions from modifying data read by the current transaction, and exclusive locks set by other transactions do not block the current transaction from reading the locked data. Allowing dirty reads can cause higher concurrency, but at the cost of reading data modifications that then are rolled back by other transactions. This may generate errors for your transaction, present users with data that was never committed, or cause users to see records twice (or not at all).

READUNCOMMITTED and NOLOCK hints apply only to data locks. All queries, including those with READUNCOMMITTED and NOLOCK hints, acquire Sch-S (schema stability) locks during compilation and execution. Because of this, queries are blocked when a concurrent transaction holds a Sch-M (schema modification) lock on the table. For example, a data definition language (DDL) operation acquires a Sch-M lock before it modifies the schema information of the table. Any concurrent queries, including those running with READUNCOMMITTED or NOLOCK hints, are blocked when attempting to acquire a Sch-S lock. Conversely, a query holding a Sch-S lock blocks a concurrent transaction that attempts to acquire a Sch-M lock.

READUNCOMMITTED and NOLOCK cannot be specified for tables modified by insert, update, or delete operations. The SQL Server query optimizer ignores the READUNCOMMITTED and NOLOCK hints in the FROM clause that apply to the target table of an UPDATE or DELETE statement.


You can minimize locking contention while protecting transactions from dirty reads of uncommitted data modifications by using either of the following:

  • The READ COMMITTED isolation level with the READ_COMMITTED_SNAPSHOT database option set ON.
  • The SNAPSHOT isolation level.
89 questions
6
votes
4 answers

Best alternative to WITH(NOLOCK)?

I'm making a self project in C#.Net, by using a 3-tier app, I wan't to make my sql statements transactional, these statements are used in several stored procedures, I've been using the WITH(NOLOCK) approach in order to query those tables that have…
jecarfor
  • 498
  • 8
  • 22
5
votes
4 answers

High volume site using ADO.NET TransactionScope vs ExecuteCommand on NOLOCK, READ UNCOMMITTED directly?

Just read this interesting article by Omar on his blog Linq to SQL solve Transaction deadlock and Query timeout problem using uncommitted reads and at the end Javed Hasan started arguing with him about his solution to the nolock situation on a…
Ray
  • 12,101
  • 27
  • 95
  • 137
5
votes
1 answer

Problem with NOLOCK on SQL 2008 using a temporary table and select statements

I am using SQL 2008 and I am getting an error most of the time I run a stored procedure. There error is: could not continue scan with nolock due to data movement. I have looked online for this and it seems that most people get this when updating…
Kyra
  • 5,129
  • 5
  • 35
  • 55
5
votes
3 answers

WTH (NOLOCK) syntax fo subquery

I am trying to add with (nolock) in a report query that when run locks the full db making imposssible for other users to use the db. I can't figure out how to use in this case: -- this is just an example: SELECT FIELDS FROM (SELECT * FROM ATABLE) AS…
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
5
votes
2 answers

In the UPDATE statement, are NOLOCK hint honored in the FROM clause?

Given the following update statement: UPDATE @TableVariable SET city = T2.city FROM @TableVariable TV INNER JOIN dbo.TABLE_1 T1 WITH (NOLOCK) ON (TV.customer_id = T1.customer_id) INNER JOIN dbo.TABLE_2 T2 WITH (NOLOCK) ON (T1.address_id =…
Jim G.
  • 15,141
  • 22
  • 103
  • 166
5
votes
1 answer

dapper-extensions GetList() with (nolock)

Does anyone know if it's possible to tell dapper to append with (nolock) when using connection.GetList()? I am using this as the R from my CQRS model and it works well but I'm concerned now we're doing a bit more heavy reading that it will…
Beannie
  • 79
  • 1
  • 5
5
votes
3 answers

How can I avoid a deadlock between these two SQL statements?

I have two stored procedures running in separate threads, running on SQL Server 2005. One procedure inserts new rows into a set of tables and the other procedure deletes old data from the same set of tables. These procedures are running into a…
Elan
  • 6,084
  • 12
  • 64
  • 84
5
votes
2 answers

with(nolock) , (nolock) , nolock differences?

I know with(nolock) and (nolock) are the same or almost the same. REF:with(nolock) or (nolock) - Is there a difference? but how about nolock? You can use either of them in select and the only notable difference I can see is when using alias that you…
Bolu
  • 8,696
  • 4
  • 38
  • 70
5
votes
1 answer

NOLOCK IN temp tables

I declared a table in my procedure which you can see below: Declare @resultTable Table ( EmpId int, EmpStatusId int, CreatedDateTime datetime ) and i perform a delete function: Delete From ActualTable Where Id = (Select EmpId from…
raberana
  • 11,739
  • 18
  • 69
  • 95
4
votes
1 answer

LINQ-To-SQL NOLOCK (NOT ReadUncommitted)

I've been searching for some time now in here and other places and can't find a good answer to why Linq-TO-SQL with NOLOCK is not possible.. Every time I search for how to apply the with(NOLOCK) hint to a Linq-To-SQL context (applied to 1 sql…
Nick Niebling
  • 317
  • 3
  • 12
4
votes
0 answers

How perform lockmode:none on associations (join) with Doctrine2 and SQL Server

I'm trying to append the With(nolock) hint to my Queries using Doctrine2. I tried using the ->setLockMode(LockMode::NONE)->getSQL(); but it didn't work as expected, it added the WITH(NOLOCK) just to the (first) table in the FROM clause. I'll…
Astaroth
  • 763
  • 6
  • 25
4
votes
2 answers

Will SQL Server NOLOCK hint return partially written rows?

First of all I'm not asking if NOLOCK should or should not be used. Let's get past that. I guess the question comes down to how sql server writes data? Is an entire row written at once or does it write it a column at a time? I'm asking because the…
user183305
3
votes
1 answer

Transaction Concurrency in C#

user 1 : begin tran select * from items with(nolock); insert into orders (odate) values(getdate()); insert into OrderData values((select max(orderid) from Orders with(nolock)),1,1); update Items set qih=qih-1 where item_id=1; select * from…
3
votes
3 answers

Why does Entity Framework ignore TransactionScope (not adding with NOLOCK)?

What am I missing? I'm trying to read with NOLOCK using a TransactionScope like this: var scopeOptions = new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted }; using (var scope = new…
Yovav
  • 2,557
  • 2
  • 32
  • 53
3
votes
3 answers

Using NOLOCK on a table which is being joined to itself

I'm working with an awful view which internally joins many, many tables together, some of which are the same table. I'm wondering, when a table is being joined to itself, how is the NOLOCK hint interpreted if it's on one of the joins and not the…
Seth
  • 342
  • 1
  • 4
  • 14