Can I Select and Update at the same time?

后端 未结 6 1624
梦谈多话
梦谈多话 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:18

    You should do three things here:

    1. Lock the row you're working on
    2. Make sure that this and only this row is locked
    3. Do not wait for the locked records: skip the the next ones instead.

    To do this, you just issue this:

    SELECT  TOP 1 *
    FROM    mytable (ROWLOCK, UPDLOCK, READPAST)
    WHERE   status = 'NEW'
    ORDER BY
            date
    
    UPDATE  …
    

    within a transaction.

提交回复
热议问题