Can I Select and Update at the same time?

后端 未结 6 1619
梦谈多话
梦谈多话 2021-01-19 00:07

This is an over-simplified explanation of what I\'m working on.
I have a table with status column. Multiple instances of the application will pull the contents of the f

6条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 00:29

    Not quite, but you can SELECT ... WITH (UPDLOCK), then UPDATE.. subsequently. This is as good as an atomic operation as it tells the database that you are about to update what you previously selected, so it can lock those rows, preventing collisions with other clients. Under Oracle and some other database (MySQL I think) the syntax is SELECT ... FOR UPDATE.

    Note: I think you'll need to ensure the two statements happen within a transaction for it to work.

提交回复
热议问题