How to update large table with millions of rows in SQL Server?

后端 未结 6 851
面向向阳花
面向向阳花 2020-11-28 10:07

I\'ve an UPDATE statement which can update more than million records. I want to update them in batches of 1000 or 10000. I tried with @@ROWCOUNT bu

6条回答
  •  执念已碎
    2020-11-28 10:32

    Your print is messing things up, because it resets @@ROWCOUNT. Whenever you use @@ROWCOUNT, my advice is to always set it immediately to a variable. So:

    DECLARE @RC int;
    WHILE @RC > 0 or @RC IS NULL
        BEGIN
            SET rowcount 5;
    
            UPDATE TableName
                SET Value  = 'abc1'
                WHERE Parameter1  = 'abc' AND Parameter2  = 123 AND Value <> 'abc1';
    
            SET @RC = @@ROWCOUNT;
            PRINT(@@ROWCOUNT)
        END;
    
    SET rowcount = 0;
    

    And, another nice feature is that you don't need to repeat the update code.

提交回复
热议问题