如何使用每日计划作业调用oracle中的存储过程? [英] how to call the stored procedure in oracle with the daily scheduled jobs?

查看:17
本文介绍了如何使用每日计划作业调用oracle中的存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 oracle 作业脚本的新手.我写了一些清除程序来清除所有旧数据并保留最近 3 个月的数据...程序已成功执行.当我手动调用时它也可以工作.流程如下:

I am new to the oracle job scripts. I wrote some purge procedure to clean all the old data and retain the last 3 months data... procedure is executed successfully. its working when im calling manually also. procedure is as follows:

CREATE OR REPLACE PROCEDURE Archive 
IS

       v_query varchar2(2048);
       v_tablename VARCHAR2(50);
       v_condition varchar2(50);
       TYPE cur_typ IS REF CURSOR;
       c cur_typ;
BEGIN 
    OPEN c for 'select tablename,columnname from pseb.purge_tables';
        FETCH c INTO v_tablename,v_condition;
        LOOP
           EXIT WHEN c%NOTFOUND;
           if(v_tablename ='cfw.DCTBLPERFCUMULATIVEMASTER') then
               v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB3MAINREG where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
               execute immediate v_query;
               v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB4TODENERG where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
               execute immediate v_query;
               v_query:='delete from cfw.DCTBLPERDFCUMULATIVEB5MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
               execute immediate v_query;
               v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB6TODREG where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
               execute immediate v_query;
               v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB7MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
               execute immediate v_query;
               v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB8MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
               execute immediate v_query;
               v_query:='delete FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
               execute immediate v_query;
           else
           begin
               v_query:='delete FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
               execute immediate v_query;
           end;
           end if;
       FETCH c INTO v_tablename,v_condition;
        end LOOP;
        close c;
END; --Procedure

我的JOb脚本如下:

begin
  DBMS_SCHEDULER.CREATE_JOB (
     job_name           =>  'purgeproc_automation',
     job_type           =>  'STORED_PROCEDURE',
     job_action         =>  'call pseb.archive();',
     repeat_interval    =>  'FREQ=DAILY;INTERVAL=2', /* every other day */
     auto_drop          => false,
     enabled            => true,
   comments           =>  'My new job');
end;
/

作业已成功创建,但作业状态为失败,未成功.背后的原因是什么?它返回以下错误:

Job was created successfully, but the job status is failed, not succeed . Whats the reason behind it? it returns the following error:

ORA-06550: line 1, column 728:
PLS-00103: Encountered the symbol "PSEB" when expecting one of the following:

   := . ( @ % ;
The symbol ":=" was substituted for "PSEB" to continue.

请指导我解决这个问题...

please guide me to solve this...

推荐答案

天哪,你的代码看起来好复杂.首先考虑这种简化:

Omg, your code looks so complicated. Consider this simplification first:

CREATE OR REPLACE PROCEDURE Archive 
IS
   v_query varchar2(2048);
BEGIN 
    FOR REC IN (select tablename,columnname condition from pseb.purge_tables)
    LOOP
       if(rec.tablename ='cfw.DCTBLPERFCUMULATIVEMASTER') then
           v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB3MAINREG where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
           execute immediate v_query;
           v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB4TODENERG where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
           execute immediate v_query;
           v_query:='delete from cfw.DCTBLPERDFCUMULATIVEB5MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
           execute immediate v_query;
           v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB6TODREG where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
           execute immediate v_query;
           v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB7MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
           execute immediate v_query;
           v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB8MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
           execute immediate v_query;
           v_query:='delete FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
           execute immediate v_query;
       else
           v_query:='delete FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
           execute immediate v_query;
       end if;
    END LOOP;
END; --Procedure

dbms_job.submit 的替代作业定义:

Alternative job definition by dbms_job.submit:

declare 
 jid number;
begin
dbms_job.submit(
    JOB => jid,
    WHAT => 'pseb.archive;', 
    NEXT_DATE => SYSDATE, 
    INTERVAL  => 'sysdate +2');
end;
/
commit; -- <<--added commit here

一种检查作业的方法:

select * from user_jobs;

这篇关于如何使用每日计划作业调用oracle中的存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆