2

Postgresql 允许我在使用以下语法选择行时锁定行:

select id, amount from table where id = 1234 for update

这使我可以更新行,同时确保也想要更新同一行的并发读取将获得更新和正确的结果。

基本上我可以做这样的事情(伪代码):

begin transaction
select id, amount from table where id = 1234 for update
if(amount == new_amount) then
  delete from table where id = 1234;
else
  update table set amount = amount - new_amount where id = 1234
end
commit transaction

Sql Server 中仅允许对 DECLARE CURSOR 使用相同的语法。有没有办法通过简单的选择来读取行,同时锁定它以进行更新?

4

1 回答 1

3
select * from T WITH (UPDLOCK)

WITH提示非常灵活且设计精良。我猜它们比for update.

于 2013-02-26T13:20:00.017 回答