In MS SQL Server, is there a way to “atomically” increment a column being used as a counter?

后端 未结 5 1333
星月不相逢
星月不相逢 2020-12-23 17:21

Assuming a Read Committed Snapshot transaction isolation setting, is the following statement \"atomic\" in the sense that you won\'t ever \"lose\" a concurrent increment?

5条回答
  •  我在风中等你
    2020-12-23 17:35

    No, it's not. The value is read in shared mode and then updated in exclusive mode, so multiple reads can occur.

    Either use Serializable level or use something like

    update t
    set counter = counter+1
    from t with(updlock, )
    where foo = bar
    

提交回复
热议问题