大数据量更新/删除模板
如果有大批量的数据要进行删除或者更新,那么使用一个循环来操作,具体模板如下:
DECLARE
@rows int,
@rows_limit int,
@row_batch int,
@row_count int
;
SELECT
@rows = 0,
@rows_limit = 记录数,
@row_batch = 轮询记录数, -- 每批处理的记录数
@row_count = @row_batch
;
WHILE @row_count = @row_batch
AND @rows < @rows_limit
BEGIN;
/*------operation statement------
--DELETE
DELETE TOP(@row_batch) SRC
FROM source_table SRC -- 源表
WHERE yourcondition = 1 -- 记录处理条件
--UPDATE
UPDATE TOP(@row_batch) SRC
SET SRC.field='
FROM source_table SRC -- 源表
WHERE SRC.field is NULL -- 记录处理条件
---------operation statement-----*/
SELECT
@row_count = @@ROWCOUNT,
@rows = @rows + @row_count
;
WAITFOR DELAY '00:00:10'; -- 每批处理之间的延时
@rows int,
@rows_limit int,
@row_batch int,
@row_count int
;
SELECT
@rows = 0,
@rows_limit = 记录数,
@row_batch = 轮询记录数, -- 每批处理的记录数
@row_count = @row_batch
;
WHILE @row_count = @row_batch
AND @rows < @rows_limit
BEGIN;
/*------operation statement------
--DELETE
DELETE TOP(@row_batch) SRC
FROM source_table SRC -- 源表
WHERE yourcondition = 1 -- 记录处理条件
--UPDATE
UPDATE TOP(@row_batch) SRC
SET SRC.field='
FROM source_table SRC -- 源表
WHERE SRC.field is NULL -- 记录处理条件
---------operation statement-----*/
SELECT
@row_count = @@ROWCOUNT,
@rows = @rows + @row_count
;
WAITFOR DELAY '00:00:10'; -- 每批处理之间的延时
END;