oracle定时运行存储过程

来源:互联网 发布:fifaonline3数据库app 编辑:程序博客网 时间:2024/05/22 15:28

创建表

create table error_msg(msg varchar2(1024),count_old number(6),stime date);
create table error_msg2(msg varchar2(1024),stime date);

insert into error_msg2 values('异常信息1',to_date('2017-11-15 12:15:40','yyyy-mm-dd hh24:mi:ss'));
insert into error_msg2 values('异常信息2',to_date('2017-11-15 12:35:40','yyyy-mm-dd hh24:mi:ss'));
insert into error_msg2 values('异常信息3',to_date('2017-11-15 12:50:40','yyyy-mm-dd hh24:mi:ss'));


insert into error_msg values('异常信息1',1,to_date('2017-11-15 12:15:40','yyyy-mm-dd hh24:mi:ss'));
insert into error_msg values('异常信息2',2,to_date('2017-11-15 12:15:40','yyyy-mm-dd hh24:mi:ss'));

创建一个存储过程

--包头

CREATE OR REPLACE PACKAGE TIMER   IS
       PROCEDURE PROC_INSERT
       ;
END TIMER;

--包体

CREATE OR REPLACE PACKAGE BODY TIMER   IS
       PROCEDURE PROC_INSERT IS
         V_COUNT1 NUMBER;
         V_COUNT2 NUMBER;
       BEGIN
         V_COUNT1:=0;
         V_COUNT2:=0;
         SELECT COUNT(1) INTO V_COUNT1 FROM ERROR_MSG2;
         SELECT COUNT_OLD INTO V_COUNT2 FROM ERROR_MSG 
                WHERE STIME=(
                             SELECT MAX(STIME) FROM ERROR_MSG
                             );
         IF V_COUNT1<>V_COUNT2 THEN 
         INSERT INTO ERROR_MSG (MSG,COUNT_OLD,STIME)
         SELECT MSG AS MSG,V_COUNT1 AS COUNT_OLD ,STIME AS STIME  FROM (SELECT MSG,STIME FROM ERROR_MSG2 
                                         WHERE STIME=(
                                                      SELECT MAX(STIME) FROM ERROR_MSG2
                                                      )
                                         );
         
         END IF;
         COMMIT;
         EXCEPTION 
           WHEN OTHERS THEN 
             ROLLBACK;
            DBMS_OUTPUT.PUT_LINE('wewewewe');
       END ;
END TIMER;

创建job

declare
  job number;
begin
  dbms_job.submit(job, 'TIMER.proc_insert;', sysdate, 'TRUNC(sysdate,''mi'') + 1 / (24*60)');

--每一分钟执行一次
commit;
end
commit;

error_msg2增加异常日志信息,(手动添加一条数据),自动加载异常信息和异常信息条总数到error_msg表中。


详细学习请参考:http://blog.csdn.net/wnantian/article/details/70057427?locationNum=6&fps=1