2012-08-24 129 views
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 

) 

我需要更新表A,其等于ID 2和的StatusCode等于“声称”更新为“订”sql server 2008更新select threadsafe?

是它线程? TKS

+0

你的意思是说原子? – Vikdor

回答

5

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

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

的等效将是包装在repeatable read transaction声明:

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 
+0

+1使用可重复读取,这是完全够用的。 – usr