创建ORACLE JOB

在项目中使用oracle JOB定期跑数据。

创建过程如下:

1、创建保存数据的目的表:

create table RPT_YD_YFTCJS_BOSS
(
  AREACODE VARCHAR2(10),
  AREANAME VARCHAR2(50),
  MONTH    CHAR(6),
  PCODE    VARCHAR2(20),
  ZWTCS    NUMBER,
  TCTD     NUMBER,
  YJ       NUMBER
);


2、创建存储过程:

create or replace procedure pd_rpt_yd_yftcjs_boss as
begin
  insert into rpt_yd_yftcjs_boss
select areacode,areaname,month,pcode,nvl(to_number(zwtcs),0) zwtcs,nvl(to_number(tctd),0) tctd,nvl(yj,0) yj from (
select t1.areacode,t1.areaname,t1.month,t1.pcode,t2.zwtcs,t3.tctd,nvl(t2.zwtcs,0)+nvl(t3.tctd,0) yj from (
select areacode,areaname,month,pcode from RPT_YD_YTCZWS_BOSS where pcode is not null and month=to_char(add_months(trunc(sysdate),-11),'yyyymm')
union
select areacode,areaname,month,pcode from RPT_YD_YTCTD_BOSS where pcode is not null  and month=to_char(add_months(trunc(sysdate),-11),'yyyymm')) t1 left join
(select areacode,areaname,month,pcode,zwtcs from RPT_YD_YTCZWS_BOSS where month=to_char(add_months(trunc(sysdate),-11),'yyyymm')) t2
on t1.areacode=t2.areacode and t1.month=t2.month and t1.pcode=t2.pcode left join
(select areacode,areaname,month,pcode,tctd from RPT_YD_YTCTD_BOSS where month=to_char(add_months(trunc(sysdate),-11),'yyyymm')) t3
on t1.areacode=t3.areacode and t1.month=t3.month and t1.pcode=t3.pcode
order by areacode,pcode desc) t1
union all
select '合计' areacode,'全国' areaname,month,'/' pcode,sum(zwtcs) zwtcs,sum(tctd) tctd,sum(yj) yj from (
select t1.areacode,t1.areaname,t1.month,t1.pcode,t2.zwtcs,t3.tctd,t2.zwtcs+t3.tctd yj from (
select areacode,areaname,month,pcode from RPT_YD_YTCZWS_BOSS where pcode is not null and month=to_char(add_months(trunc(sysdate),-11),'yyyymm')
union
select areacode,areaname,month,pcode from RPT_YD_YTCTD_BOSS where pcode is not null and month=to_char(add_months(trunc(sysdate),-11),'yyyymm')) t1 left join
(select areacode,areaname,month,pcode,zwtcs from RPT_YD_YTCZWS_BOSS where month=to_char(add_months(trunc(sysdate),-11),'yyyymm')) t2
on t1.areacode=t2.areacode and t1.month=t2.month and t1.pcode=t2.pcode left join
(select areacode,areaname,month,pcode,tctd from RPT_YD_YTCTD_BOSS where month=to_char(add_months(trunc(sysdate),-11),'yyyymm')) t3
on t1.areacode=t3.areacode and t1.month=t3.month and t1.pcode=t3.pcode
order by areacode,pcode desc)
group by month;
end;

3、JOB 每月定时执行   每月3日凌晨10点执行
variable job1 number;
begin
dbms_job.submit(:job1,'pd_rpt_yd_yftcjs_boss;',sysdate,'TRUNC(LAST_DAY(SYSDATE))+3+10/24');
end;

以上都在PL/SQL common windows命令行下执行。

报Error: PLS-00103: 出现符号 ";"在需要下列之一时:。。。。。。

去掉脚本前的空格就可以了


你可能感兴趣的:(创建ORACLE JOB)