Don’t blink you might READPAST it

Following on from my previous post on NOLOCK ( I want to talk about another hint called READPAST. This hint tells the database engine not to read rows that are locked by other transactions.

It is best explained with an example.

First let’s look at the basics of locking under the default isolation level with no hints. In my first SSMS (SQL Server Management Studio) window I issue the following.

UPDATE [HumanResources].[Department]
SET Name = 'Global'
WHERE DepartmentID = 16

Under a second SSMS connection I run a select statement – yes naturally I am blocked.

SELECT * FROM HumanResources.Department


I have circled in red the resource_description which can be used as input into the undocumented function  %%lockres%% to understand/confirm what is actually being locked.

select *,%%lockres%%
from    HumanResources.Department (nolock)
where    %%lockres%% IN('(62fb4a4c0e9e)')

Just to confirm it is the DepartmentID = 16 row as per my update statement.


Anyways, I issue a rollback and the blocking stops.


So now that we understand what “normally” happens let’s play with the READPAST hint.

SSMS window 1

UPDATE [HumanResources].[Department]
SET Name = 'Global'
WHERE DepartmentID = 16

SSMS window 2

 SELECT * FROM HumanResources.Department (READPAST)


Totally skips the row where DepartmentID =16.

If I rollback the update statement you should see the DepartmentID = 16 come back.



SELECT * FROM HumanResources.Department (READPAST)


So, as the name of the hint suggests, it literally does “readpast” it.