oracle定期删除表分区存储过程

 

  1. set serveroutput on  
  2. dbms_output.put_line()
  3. exec 存储过程

信息表: 

create table T_DROP_PAR_LIST
(
  ID               number not null,
  TABLE_NAME       VARCHAR2(60) not null,
  SAVE_DAYS        number not null,
  IS_DROP          char(1) not null,
  DROP_PAR_TIME    DATE

);
-- Add comments to the table 
comment on table T_DROP_PAR_LIST is '待删除表分区信息表';
-- Add comments to the columns 
comment on column T_DROP_PAR_LIST.ID
  is '序号ID';
comment on column T_DROP_PAR_LIST.TABLE_NAME
  is '表名';
comment on column T_DROP_PAR_LIST.SAVE_DAYS
  is '保留天数';
comment on column T_DROP_PAR_LIST.IS_DROP
  is '是否删除,1代表删除,0不删除';
comment on column T_DROP_PAR_LIST.DROP_PAR_TIME
  is '删除时间';

 

create or replace procedure DROP_PART_DAYS
AS
 v_SqlExec VARCHAR2(200); 

  --表的游标定义
  cursor tb_cursor is
    SELECT table_name,save_days from T_DROP_PAR_LIST where is_drop=1;  --记录要删除分区的表名,保留多长时间
    v_tb tb_cursor%rowtype;
  --表分区的游标定义
cursor par_cursor(tb_name varchar2,save_days number) is
  select partition_name
   from user_tab_partitions
   where table_name= tb_name and partition_name not like '%MAX%'
   and to_date(REPLACE(SUBSTR(partition_name,length(partition_name)-8,length(partition_name)),'_',''),'YYYYMMDD')< sysdate- save_days
   order by partition_name;
begin
  open   tb_cursor;
     loop
       fetch tb_cursor into v_tb;
       exit when tb_cursor%notfound;
           for par_cur in par_cursor(v_tb.table_name,v_tb.save_days) 
             loop
              v_SqlExec:='ALTER TABLE '|| v_tb.table_name ||' DROP PARTITION '|| par_cur.partition_name ;
             -- dbms_output.put_line(v_SqlExec);
              DBMS_Utility.Exec_DDL_Statement(v_SqlExec);
             end loop;
              update t_drop_par_list set drop_par_time = sysdate where table_name=v_tb.table_name ;
              commit;
      end loop;
  close tb_cursor;

 END DROP_PART_DAYS;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值