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;
/*--删除主数据表上非本月记录
delete from test_ad where patroldate<to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyymmdd'),'yyyymmdd');
--删除历史表本月记录
delete from test_pd where patroldate<to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyymmdd'),'yyyymmdd');
--删除历史表本月记录
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;
--=============注意!在存储过程中使用CREATE或DROP需要显示授权==========
--grant create table to user,grant drop any table to user
/
create or replace procedure PASUPPLYPROC is
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;
--=============注意!在存储过程中使用CREATE或DROP需要显示授权==========
--grant create table to user,grant drop table to user
/