sql 清除超过一周的备份表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 清除超过一周的备份表相关的知识,希望对你有一定的参考价值。

set SERVEROUTPUT ON
-- Created by: AmrAbdeen @ 30/03/2019 06:28 PM 
-- procedure job is: PURGE all backup tables which created from 1 week before
-- All backup tables which contained their name with this text "_DEL#ME_DDMM"

declare
    cursor tbls_cur
    is
           --Getting the backup tables 
            with t1 as (
                select 
                table_name,
                substr(table_name,instr( lower(table_name),'_del#me_') +8,4 ) as date_
                from all_tables
                where lower(table_name) like '%_del#me%'
            ),
            
            t2 as(
            select 
                table_name,
                date_,
                pkg_common.is_number(date_) as is_number
            from t1
            )
            
            select 
                to_date(date_,'ddmm') as table_created_date,
                table_name
            from t2
            where is_number = 1;

begin
    for i in tbls_cur
    loop
    
        if trunc(sysdate) >= trunc(i.table_created_date + 7) -- That means collect the tables which created from 1 week before.
        then
            dbms_output.put_line(i.table_name);
            execute immediate
            'drop table "ERP_SCH"."'|| i.table_name ||'" cascade constraints PURGE';
        end if;
    end loop;
end;

以上是关于sql 清除超过一周的备份表的主要内容,如果未能解决你的问题,请参考以下文章

如何从oracle sql中的表中找到一周的峰值

我怎样才能获得超过一周的推文(使用 tweepy 或其他 python 库)

sql server 2000 日志备份

SQL - 灵活地确定前一周的最后一天

SQL2005自动备份,定期删除的维护计划及自动定期清除日志

sql语句 查询一个月的几个周的数据