每月新增数据百万多条,需要定期处理2个主要数据表(test_ad,test_pd),移动非当月数据到历史表中保存
数据操作存储过程如下:
MYPROC.prc
create or replace procedure MYPROC is
TableName_AD char(13);
TableName_PD char(13);
tmp_str varchar2(100);
tmp_str2 varchar2(100);
tmp_str3 varchar2(100);
tmp_str4 varchar2(100);
tmp_str5 varchar2(100);
tmp_str6 varchar2(100);
tmp_str7 varchar2(100);
tmp_str8 varchar2(100);
begin
--临时表名赋值
if TableName_AD is null then
select 'PA_AD_'||to_char(add_months(sysdate,-1),'yyyymm') into TableName_AD from dual;
end if;
if TableName_PD is null then
select 'PA_PD_'||to_char(add_months(sysdate,-1),'yyyymm') into TableName_PD from dual;
end if;
--创建(test_ad)历史表
tmp_str:='create table '||TableName_AD||' as select * from test_ad where patroldate<'''||to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyy-mm-dd'),'yyyy-mm-dd')||'''';
execute immediate tmp_str;
--创建(test_pd)历史表
tmp_str2:='create table '||TableName_PD||' as select * from test_pd where patroldate<'''||to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyy-mm-dd'),'yyyy-mm-dd')||'''';
execute immediate tmp_str2;
--commit;
--创建本月数据临时表
tmp_str3:='create table temp_ad as select * from test_ad Where patroldate > ='''||to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyymmdd'),'yyyymmdd')||'''';
execute immediate tmp_str3;
tmp_str4:='create table temp_pd as select * from test_pd Where patroldate > ='''||to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyymmdd'),'yyyymmdd')||'''';
execute immediate tmp_str4;
--删除本月数据表(test_ad,test_pd)
tmp_str5:='drop TABLE test_ad';
execute immediate tmp_str5;
tmp_str6:='drop TABLE test_pd';
execute immediate tmp_str6;
--本月数据临时表重命名表(test_ad,test_pd)
tmp_str7:='rename temp_ad to test_ad';
tmp_str8:='rename temp_pd to test_pd';
execute immediate tmp_str7;
execute immediate tmp_str8;
/*--删除主数据表上非本月记录 --删除历史表本月记录 --插入操作记录 --=============注意!在存储过程中使用CREATE或DROP需要显示授权========== 以上存储过程每月3号定期执行 declare job1 number; 为了补救意外导致3号午夜12点没有执行MYPROC 使用另外一个存储过程验证MYPROC是否执行 PASUPPLYPROC.prc create or replace procedure PASUPPLYPROC is --=============注意!在存储过程中使用CREATE或DROP需要显示授权========== 第二个job定期执行PASUPPLYPROC验证 declare job1 number; DBA_JOBS
delete from test_ad where patroldate
delete from test_pd where patroldate
tmp_str2:='delete from '||tmp_TableName||' where patroldate>='||to_date(to_char(add_months(last_day(sysdate) +1,-1),'yyyymmdd'),'yyyymmdd');
execute immediate tmp_str2;
*/
insert into oper_proc_log values(sysdate,'pc',0);
commit;
end MYPROC;
--grant create table to user,grant drop any table to user
/
begin
--每月3号午夜12点执行MYPROC
dbms_job.submit(job1,'MYPROC;',sysdate,'TRUNC(LAST_DAY(SYSDATE ) + 3)');
commit;
end;
isnull integer;
tmp_str varchar2(100);
begin
--取得本月执行myproc次数
if isnull is null then
select count(*) into isnull from oper_proc_log
where oper_date>=to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyymmdd'),'yyyymmdd');
end if;
--无本月执行记录则立即执行myproc
if isnull=0 then
tmp_str:='begin myproc; end;';
execute immediate tmp_str;
commit;
end if;
end PASUPPLYPROC;
--grant create table to user,grant drop table to user
/
begin
--每月15号午夜12点10分执行PASUPPLYPROC
dbms_job.submit(job1,'PASUPPLYPROC;',sysdate,'TRUNC(LAST_DAY(SYSDATE) + 14) +(24*60+10)/(24*60)');
commit;
end;
===========================================
字段(列) 类型 描述
JOB NUMBER 任务的唯一标示号
LOG_USER VARCHAR2(30) 提交任务的用户
PRIV_USER VARCHAR2(30) 赋予任务权限的用户
SCHEMA_USER VARCHAR2(30) 对任务作语法分析的用户模式
LAST_DATE