Can I Select and Update at the same time?

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

    Yes, and maybe use the rowlock hint to keep it isolated from the other threads, eg.

    UPDATE
    Jobs WITH (ROWLOCK, UPDLOCK, READPAST)
    SET Status = 'WORKING'
    WHERE JobID =
    (SELECT Top 1 JobId FROM Jobs WHERE Status = 'NEW')
    

    EDIT: Rowlock would be better as suggested by Quassnoi, but the same idea applies to do the update in one query.

提交回复
热议问题