views:

32

answers:

1

With SQL Server 2008, how can I detect if a record is locked?

EDIT:

I need to know this, so I can notify the user that the record is not accessible because the record is blocked.

+2  A: 

In most circumstances with SQL 2008 you can do something like:

if exists(select 0 from table with (nolock) where id = @id) 
   and not exists(select 0 from table with(readpast) where id = @id)
begin
  -- Record is locked! Do something.

end

If that is not enough (that is, you need to ignore table-level locks as well), use the NOWAIT hint that throws an error if there's a lock.

GSerg
Thank you so much. What I needed was the NOWAIT hint!
Martijn