如何使dbms_scheduler.create_job可配置?

时间:2019-02-28 09:32:40

标签: oracle plsql dbms-scheduler

我想使其可配置,而不是对值dbms_scheduler.create_job repaeat_interval进行硬编码。因此,我创建了一个新表,该表允许配置作业的repeat_interval。

下面的过程将列作为输入参数:

    PROCEDURE scheduled_job IS
      v_start_date swpurge_job_config.start_date%TYPE;
      v_freq       swpurge_job_config.freq%TYPE;
      v_byday      swpurge_job_config.byday%TYPE;
      v_byhour     swpurge_job_config.byhour%TYPE;
      v_byminute   swpurge_job_config.byminute%TYPE;
      v_bysecond   swpurge_job_config.bysecond%TYPE;
      v_end_date   swpurge_job_config.end_date%TYPE;
   BEGIN
      SELECT start_date
            ,freq
            ,byday
            ,byhour
            ,byminute
            ,bysecond
            ,end_date
        INTO v_start_date
            ,v_freq
            ,v_byday
            ,v_byhour
            ,v_byminute
            ,v_bysecond
            ,v_end_date
        FROM swpurge_job_config;
      dbms_scheduler.create_job(job_name        => 'Purge_MW_Log_Data'
                               ,job_type        => 'STORED_PROCEDURE'
                               ,job_action      => 'DBA_UTILS_OWNER.PURGE_TABLES'
                               ,start_date      => 'SYSTIMESTAMP'
                               ,repeat_interval => 'freq' || '=' ||
                                                   v_freq || '; ' ||
                                                   'byday' || '=' ||
                                                   v_byday || '; ' ||
                                                   'byhour' || '=' ||
                                                   v_byhour || '; ' ||
                                                   'byminute' || '=' ||
                                                   v_byminute || '; ' ||
                                                   'bysecond' || '=' ||
                                                   v_bysecond || ';'
                               ,end_date        => NULL
                               ,enabled         => TRUE
                               ,auto_drop       => FALSE
                               ,comments        => 'runs weekly and looks for partitions that needs to be purged');

   END scheduled_job;

不知道该怎么办。任何帮助表示赞赏。

错误消息:

ORA-01858:在期望数字的地方找到了非数字字符 ORA-06512:在“ DBA_UTILS_OWNER.SWPURGE”行450>“ dbms_scheduler.create_job(job_name =>'Purge_MW_Log_Data'”

1 个答案:

答案 0 :(得分:0)

DECLARE
   PROCEDURE scheduled_job IS
      v_start_date swpurge_job_config.start_date%TYPE;
      v_freq       swpurge_job_config.freq%TYPE;
      v_byday      swpurge_job_config.byday%TYPE;
      v_byhour     swpurge_job_config.byhour%TYPE;
      v_byminute   swpurge_job_config.byminute%TYPE;
      v_bysecond   swpurge_job_config.bysecond%TYPE;
      --v_end_date   swpurge_job_config.end_date%TYPE;
   BEGIN
      SELECT start_date
            ,freq
            ,byday
            ,byhour
            ,byminute
            ,bysecond
           -- ,end_date
        INTO v_start_date
            ,v_freq
            ,v_byday
            ,v_byhour
            ,v_byminute
            ,v_bysecond
         --   ,v_end_date
        FROM swpurge_job_config;
      dbms_scheduler.create_job(job_name        => 'Purge_MW_Log_Data'
                               ,job_type        => 'STORED_PROCEDURE'
                               ,job_action      => 'OWNER.PROCEDURE'
                               ,start_date      => v_start_date
                               ,repeat_interval => 'freq' || '=' ||
                                                   v_freq || '; ' ||
                                                   'byday' || '=' ||
                                                   v_byday || '; ' ||
                                                   'byhour' || '=' ||
                                                   v_byhour || '; ' ||
                                                   'byminute' || '=' ||
                                                   v_byminute || '; ' ||
                                                   'bysecond' || '=' ||
                                                   v_bysecond || ';'
                               ,end_date        => NULL
                               ,enabled         => TRUE
                               ,auto_drop       => FALSE
                               ,comments        => 'something');

   END scheduled_job;

BEGIN

   scheduled_job();
END;
/

问题是开始日期前后的单引号。还要注意的另一件事是,不允许将v_end_date解析为dbms_scheduler中的参数(不确定原因),但这对我来说已经足够了。