How can I do a SQL UPDATE in batches, like an Update Top?

后端 未结 5 2122
情书的邮戳
情书的邮戳 2020-12-31 02:39

Is it possible to add a TOP or some sort of paging to a SQL Update statement?

I have an UPDATE query, that comes down to something like this:

         


        
5条回答
  •  清酒与你
    2020-12-31 03:19

    Depending on your ability to change the datastructure of the table, I would suggest that you add a field to your table that can hold some sort of batch-identificator. Ie. it can be a date-stamp if you do it daily, an incremenal value or basically any value that you can make unique for your batch. If you take the incremental approach, your update will then be:

    UPDATE TOP (100000) XXX SET XXX.BATCHID = 1, XXX.YYY = ....
    ...
    WHERE XXX.BATCHID < 1 
      AND (rest of WHERE-clause here).
    

    Next time, you'll set the BATCHID = 2 and WHERE XXX.BATCHID < 2

    If this is to be done repeatedly, you can set an index on the BATCHID and reduce load on the server.

提交回复
热议问题