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

后端 未结 6 867
面向向阳花
面向向阳花 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:33

    First of all, thank you all for your inputs. I tweak my Query - 1 and got my desired result. Gordon Linoff is right, PRINT was messing up my query so I modified it as following:

    Modified Query - 1:

    SET ROWCOUNT 5
    WHILE (1 = 1)
      BEGIN
        BEGIN TRANSACTION
    
            UPDATE TableName 
            SET Value = 'abc1' 
            WHERE Parameter1 = 'abc' AND Parameter2 = 123
    
            IF @@ROWCOUNT = 0
              BEGIN
                    COMMIT TRANSACTION
                    BREAK
              END
        COMMIT TRANSACTION
      END
    SET ROWCOUNT  0
    

    Output:

    (5 row(s) affected)
    
    (5 row(s) affected)
    
    (4 row(s) affected)
    
    (0 row(s) affected)
    

提交回复
热议问题