I want to do the following in one transaction:
transaction isolation is set to read-commited, server is SQL server.
How can I guarantee that another transaction does not write a different value to the column after I read it? Will the server reject my write if another transaction changed the column?
In other words, can SQL server be used as a distributed lock on a given column?
Who said you have to read it first?
UPDATE yourtable
SET yourcolumn = CASE
WHEN certaincondition = 1 THEN 'newvalue'
ELSE yourcolumn
END
WHERE id = 'yourid'
You evaluate inside the UPDATE
itself. It's guaranteed to be totally isolated. You can have multiple of this same UPDATE
running from different instances, the transactions will be queued and processed one by one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With