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
同じ構文は、DECLARECURSORの場合にのみSQLServerで許可されます。単純な選択で更新のためにロックしながら行を読み取る方法はありますか?