Return unlocked rows in a “select top n” query

后端 未结 4 2056
再見小時候
再見小時候 2021-01-02 18:17

I need to have a MsSql database table and another 8 (identical) processes accessing the same table in parallel - making a select top n, processing those n rows, and updating

4条回答
  •  孤独总比滥情好
    2021-01-02 18:55

    The most simplest method is to use row locking:

    BEGIN TRAN
    
    SELECT *
    FROM authors
    WITH (HOLDLOCK, ROWLOCK)
    WHERE au_id = '274-80-9391'
    
    /* Do all your stuff here while the record is locked */
    
    COMMIT TRAN
    

    But if you are accessing your data and then closing the connection, you won't be able to use this method.

    How long will you be needing to lock the rows for? The best way might actually be as you say to place a counter on the rows you select (best done using OUTPUT clause within an UPDATE).

提交回复
热议问题