is there an SQL Server equivalent of PostgreSQL “select * for update” without opening cursor
Postgresql allows me to lock the row while selecting it with the following syntax:
select id, amount from table where id = 1234 for update
This allows me to update the row while ensuring that concurrent reads who also want to update the same row will get updated and correct results.
Basically I can do something like this (pseudocode):
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
The same syntax is allowed in Sql Server only for DECLARE CURSOR. Is there a way to read the row while locking it for update with just simple select?
One Solution collect form web for “is there an SQL Server equivalent of PostgreSQL “select * for update” without opening cursor”
select * from T WITH (UPDLOCK)
WITH hints are very flexible and well designed. I guess they are more precise and general than