﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server Newbies  / atomic value update? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 19:48:48 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: atomic value update?</title><link>http://www.sqlservercentral.com/Forums/Topic1403899-1291-1.aspx</link><description>[quote][b]aerojockey (1/7/2013)[/b][hr]I have an application that needs to make an atomic update to an item in a table.  It has to read the value, do something complicated to it, and write it back, and in the meantime no one else should be able to overwrite the value.  So what I'm doing is like this:[code]SELECT state FROM tab WHERE id=7[/code]Then the program does something complex based on value of state.  Then:[code]UPDATE tab SET state=some_new_value WHERE id=7[/code]Problem is, between the SELECT and UPDATE, some other user can come in an overwrite state, because the table hasn't been locked at that point.My solution was to make a trivial update to the table first, like this:[code]UPDATE table SET id=7 WHERE id=7[/code]This locks the table from overwriting by other users.  Then I can proceed with the SELECT, and COMMIT the transaction after the UPDATE.  This does what I want to, mostly, but it feels icky, and I am afraid it make my code prone to unpleasant corner cases if another user deletes the row.This app is otherwise vanilla SQL with simple data requirements.  I don't know much about SQL Server in particular.Thanks for any suggestions.[/quote]This can be done as follows (designed exactly for this case):[code="sql"]BEGIN TRAN T1;SELECT state FROM tab WHERE id=7 WITH (UPDLOCK);UPDATE tab SET state=some_new_value WHERE id=7;COMMIT TRAN T1;[/code]</description><pubDate>Mon, 07 Jan 2013 22:48:46 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>atomic value update?</title><link>http://www.sqlservercentral.com/Forums/Topic1403899-1291-1.aspx</link><description>I have an application that needs to make an atomic update to an item in a table.  It has to read the value, do something complicated to it, and write it back, and in the meantime no one else should be able to overwrite the value.  So what I'm doing is like this:[code]SELECT state FROM tab WHERE id=7[/code]Then the program does something complex based on value of state.  Then:[code]UPDATE tab SET state=some_new_value WHERE id=7[/code]Problem is, between the SELECT and UPDATE, some other user can come in an overwrite state, because the table hasn't been locked at that point.My solution was to make a trivial update to the table first, like this:[code]UPDATE table SET id=7 WHERE id=7[/code]This locks the table from overwriting by other users.  Then I can proceed with the SELECT, and COMMIT the transaction after the UPDATE.  This does what I want to, mostly, but it feels icky, and I am afraid it make my code prone to unpleasant corner cases if another user deletes the row.This app is otherwise vanilla SQL with simple data requirements.  I don't know much about SQL Server in particular.Thanks for any suggestions.</description><pubDate>Mon, 07 Jan 2013 15:48:32 GMT</pubDate><dc:creator>aerojockey</dc:creator></item></channel></rss>