I'm writing a synchronizer software which will take all changes in one DB and synchronize them to another DB. To this end I've added in my table T
two columns:
alter table T add LastUpdate rowversion, LastSync binary(8) not null default 0
Now I can easily select all rows that have changed since the last synchronization:
select * from T where LastUpdate > LastSync
However after performing the synchronization I should make the two fields equal. But updating the row also updates the timestamp, so I must do this:
update T set LastSync=@@DBTS+1 where ID=@syncedId
But I'm wondering - will this always work? What if I read the value of @@DBTS
and then another user manages to insert/update a row somewhere before my row is committed? Is this risky code? And if yes - how could it be made better?