2
UPDATE dbo.A  
  SET StatusCode = 'booked' 
   , UpdateDate = GETDATE()
 OUTPUT INSERTED.id INTO @TableVar
 WHERE id = ( 
     SELECT TOP 1 wq.id
     FROM dbo.A AS wq
     WHERE wq.statusCode = 'Claimed' and wq.id = 2

)

我需要更新 id 等于 2 且 statusCode 等于 'Claimed' 的表 A 更新为 'booked'

它是线程安全的吗?tks

4

1 回答 1

5

with (updlock, holdlock)您可以通过添加到子查询来提高并发安全性:

FROM dbo.A AS wq with (updlock, holdlock)

等效的方法是将语句包装在repeatable read事务中:

REPEATABLE READ
Specifies that statements cannot read data that has been modified but not yet 
committed by other transactions and that no other transactions can modify data 
that has been read by the current transaction until the current transaction 
completes.

这看起来像:

set transaction isolation level repeatable read
start transaction
... your query here ...
commit transaction
于 2012-08-24T12:58:57.630 回答