2010-09-22 43 views
0

在SQL Server 2008中,如何检测记录是否被锁定?如何检测记录被锁定?

编辑:

我需要知道这一点,所以我可以通知,因为该记录被阻塞的记录是无法访问的用户。

+0

你能否进一步解释你在问什么?如同为什么你需要知道记录是否被锁定? – 2010-09-22 10:04:04

+0

看到我的编辑在startpost – Martijn 2010-09-22 10:09:12

+1

我认为你可能会以错误的方式来解决这个问题:消除锁定,而不是集中报告给用户 – 2010-09-22 10:37:12

回答

3

在大多数情况下与SQL 2008,你可以这样做:

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 

如果这是不够的(也就是你需要忽略表级锁为好),使用NOWAIT提示抛出如果有锁,则为错误。

+0

非常感谢。我需要的是NOWAIT暗示! – Martijn 2010-09-22 11:09:08