2012-08-24 199 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