我有一个数据库,每天用增量数据填充数据,然后在每个月的月底将该月的数据全部下载到系统中。我们的业务希望每一天都投入到系统中,然后在每个月的月底将日常资料移除并留下完整的月份数据。我已经写下了下面的查询,如果你能帮助,我会很感激。使用while循环清除数据。
DECLARE @looper INT
DECLARE @totalindex int;
select name, (substring(name,17,8)) as Attempt, substring(name,17,4) as [year], substring(name,21,2) as [month], create_date
into #work_to_do_for
from sys.databases d
where name like 'Snapshot%' and
d.database_id >4 and
(substring(name,21,2) = DATEPART(m, DATEADD(m, -1, getdate()))) AND (substring(name,17,4) = DATEPART(yyyy, DATEADD(m, -1, getdate())))
order by d.create_date asc
SELECT @totalindex = COUNT(*) from #work_to_do_for
SET @looper = 1 -- reset and reuse counter
WHILE (@looper < @totalindex)
BEGIN;
set @[email protected]+1
END;
DROP TABLE #work_to_do_for;
我需要执行几个表上的清除。
在此先感谢。
大约每月有多少条记录被清除? – rwking
它可能是每个快照数百万。 – mhodges
好的。而你正在试图用你发布的查询完成什么? – rwking