SQL Server 2008: SELECT FOR UPDATE

前端 未结 4 1248
时光说笑
时光说笑 2021-01-13 23:28

I have seen a question on here about this however it was old so I will ask again in case a solution now exists.

My issue is this. I have a database table which I wi

4条回答
  •  甜味超标
    2021-01-14 00:18

    You need to use one of the so-called table hints:

    The update lock prevents other processes from attempting to update or delete the rows in question - but it does not prevent read access:

        SELECT TOP (20) * 
        FROM [TMA_NOT_TO_ENTITY_QUEUE] WITH (UPDLOCK)
        WHERE [TMA_NOT_TO_ENTITY_QUEUE].[STATE_ID] = 2 
        ORDER BY TMA_NOT_TO_ENTITY_QUEUE.ID
    

    There's also an exclusive lock, but basically, the update lock should be enough. Once you've selected your rows with an update lock, those rows are "protected" against updates and writes until your transaction ends.

提交回复
热议问题