【问题标题】:Doing an efficient update in batches using Backup table使用备份表进行高效的批量更新
【发布时间】:2016-01-13 19:04:14
【问题描述】:

我有一些从 tableA 备份的数据。所以我通过执行以下操作来做到这一点:-

select * into backuptable_tableA from tableA
where column1= 'Value1' and column2 = 'Value2'

备份由大约 185,000 行组成,如上所示,称为 backuptable_tableA

现在在 tableA 中,我需要使用值“LAMK”更新 1 列(column1)。现在任何人都可以建议一个有效的 SQL 查询,它可以更新 tableA 中的 column1,但可以批量更新。假设一次 10,000 行? SQL 应该在更新前 10,000 个后停止,然后我可以检查数据并执行下一个 10,000 个等等......无论如何也可以通过加入 backuptable_tableA 来确保它做到这一点? (为了论证,假设 tableA 作为 column1、column2、column3 和 column4 的唯一约束)。

..或者我是否必须在进行备份时使用的更新中使用相同的参数?

谢谢

【问题讨论】:

    标签: sql sql-server sql-server-2008 tsql sql-update


    【解决方案1】:

    过去使用 while 循环对我有用的东西 https://msdn.microsoft.com/en-gb/library/ms178642.aspx

    set rowcount 10000
    declare @rc int
    set @rc =1 
    while @rc !=0
    begin
    
      update TableA set column1 = 'LAMK'
        where column1 != 'LAMK'
          and column2  = ... 
          and column3  = ... 
          and column4  = ...
      select @rc = @@rowcount
    end
    go
    

    您可以在循环内添加检查并根据需要回滚事务。

    HTH

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 2021-01-24
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多