ORACLE自动创建分区

背景介绍

ORACLE设定按日期的分区表,并保证每日均有15天的富余。如若某天分区创建执行失败,创建分区时需将失败那天的分区补充创建。

实现方式

每天按照最大分区同当前时间比对判断,查看缺失多少天,然后循环创建分区。

示例代码

--创建存储过程
create or replace procedure zxjy_ora.add_wen_pt
as
  v_sql1 varchar2(2000);
  maxname varchar2(200);
  intervalDay integer;
  now integer;
  yestoday integer;
  maxDay integer := 15;
begin
  select PARTITION_NAME into maxname from (select substr(partition_name,2,8) as  PARTITION_NAME from user_tab_partitions where table_name='WEN' and PARTITION_NAME <> 'PMAX'   order by PARTITION_NAME desc) where rownum = 1;
  select trunc(to_date(maxname,'yyyymmdd'))-trunc(sysdate) into intervalDay from dual;
  for i in intervalDay .. maxDay-1 loop
    select to_number(to_char(sysdate+i+2,'yyyymmdd')) into now from dual;
    select to_number(to_char(sysdate+i+1,'yyyymmdd')) into yestoday from dual;
    v_sql1:='alter table ZXJY_ORA.WEN split partition pmax at ('||now||') into (PARTITION p'||yestoday||',PARTITION pmax)';
    execute immediate v_sql1;
  end loop;
end add_wen_pt;
/

---创建调度
BEGIN
DBMS_SCHEDULER.CREATE_SCHEDULE (
schedule_name     => 'sch_add_wen_pt',
start_date        => SYSDATE,
repeat_interval   => 'FREQ=DAILY; BYHOUR=22;BYMINUTE=00;BYSECOND=0',
comments          => 'EACH ONE DAYS ADD A NEW PARTITION ');
end;
/

--创建作业执行调度
BEGIN 
DBMS_SCHEDULER.CREATE_JOB ( 
job_name => 'JOB_ADD_WEN_PT', 
job_type => 'STORED_PROCEDURE', 
job_action => 'zxjy_ora.add_wen_pt', 
schedule_name => 'sch_add_wen_pt', enabled => true); 
END; 
/
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页