我有一個數據庫,每天用增量數據填充數據,然後在每個月的月底將該月的數據全部下載到系統中。我們的業務希望每一天都投入到系統中,然後在每個月的月底將日常資料移除並留下完整的月份數據。我已經寫下了下面的查詢,如果你能幫助,我會很感激。使用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