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;