gpt4 book ai didi

sql - 更新行 par lot SQL Server

转载 作者:行者123 更新时间:2023-12-04 16:17:27 26 4
gpt4 key购买 nike

我需要更改表中列的值,所以我需要这样做:

update members
set frequence = 1
where frequence <> 1

我有 700 万行受到影响,我需要分批更新 100000 行,然后休眠 2 分钟(复制到其他数据库的时间)。

请问我该怎么做?

注册

最佳答案

这是一种比较常见的方法,尽管通常它被用于limit the impact on transaction logs for large delete operations。 :

SET NOCOUNT ON;
DECLARE @r INT;
SET @r = 1;

WHILE @r > 0
BEGIN
BEGIN TRANSACTION;

update top (100000) members
set frequence = 1
where frequence <> 1

SET @r = @@ROWCOUNT;

COMMIT TRANSACTION;

WAITFOR DELAY '00:02'

-- To limit log activity:
-- CHECKPOINT; -- if simple
-- BACKUP LOG ... -- if full
END

如果您需要按顺序执行它们,请这样做:

SET NOCOUNT ON;
DECLARE @r INT;
SET @r = 1;

WHILE @r > 0
BEGIN
BEGIN TRANSACTION;

;with CTE as (
select top 100000 *
from members
where frequence <> 1
order by member_id
)
update CTE
set frequence = 1

SET @r = @@ROWCOUNT;

COMMIT TRANSACTION;

WAITFOR DELAY '00:02'

-- To limit log activity:
-- CHECKPOINT; -- if simple
-- BACKUP LOG ... -- if full
END

关于sql - 更新行 par lot SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44412490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com