如何从DB表中检索日历表达式

时间:2013-08-21 20:35:37

标签: plsql triggers

我正在用一个确定的repeat_interval做一份工作。我的目标是从表中检索此值,以便我可以在表中更改此值并相应地修改作业。为此,我做了以下触发器:

CREATE OR REPLACE TRIGGER config_table_aur AFTER
  UPDATE OF value ON config_table FOR EACH row WHEN (new.property = 'job_interval') DECLARE v_job NUMBER;
  BEGIN
    dbms_job.submit (v_job, 'begin         
update_interval (' || :new.value || ');       
end;');
  END;

此触发器调用以下过程:

CREATE OR REPLACE
PROCEDURE update_interval(
    p_new_interval IN config_table.value%type)
AS
BEGIN
  dbms_scheduler.set_attribute ('jobu', 'repeat_interval', p_new_interval);
END update_interval;

其中p_new_interval是我从表中检索的值。我遇到的问题是,如果我尝试在表格中设置这样的值:

FREQ=DAILY; INTERVAL=1;

然后我收到一个错误说:

Fila 1: ORA-06550: line 2, column 46:
PLS-00103: Encountered the symbol ";" when expecting one of the following:

   year month day hour minute second
The symbol ";" was ignored.
ORA-06512: at "SYS.DBMS_JOB", line 82
ORA-06512: at "SYS.DBMS_JOB", line 140
ORA-06512: at "SOMESCHEMA.CONFIG_TABLE_AUR", line 3
ORA-04088: error during execution of trigger 'SOMESCHEMA.CONFIG_TABLE_AUR'

我猜问题是属性值包含分号';'因为如果我不使用它们,我就不会收到错误。

你有什么建议可以解决这个问题吗?

谢谢

1 个答案:

答案 0 :(得分:0)

  

我猜问题是属性值包含分号';'因为如果我不使用它们,我就不会收到错误。

     

你有什么建议可以解决这个问题吗?

呃...你的问题毫无意义。您知道问题,但是您不想修复repeat_interval calendaring syntax中的语法错误吗?

对于这个简单的示例,您的触发器看起来不必要复杂(但您可能有正当理由使用DBMS_JOB)。下面是一个示例,首先将计划作业设置为在第30秒的每分钟运行,然后通过配置表将repeat_interval更改为每10秒运行一次:

--
-- scheduler configuration via tailored config table
--

create table scheduler_config (
  Job_name varchar2(100) not null,
  repeat_interval varchar2(100) not null
);

insert into scheduler_config values('logger1', 'FREQ=SECONDLY; BYSECOND=30');
commit;

create or replace trigger scheduler_config_trg
after update of repeat_interval
on scheduler_config
for each row
declare
  pragma autonomous_transaction;
begin
  -- Note: throws an exception if no such job
  dbms_scheduler.set_attribute(name => :new.job_name,
                               attribute => 'repeat_interval',
                               value => :new.repeat_interval);
end;
/
show errors

--
-- a simple job we want to schedule
--

create table scheduler_log (
  job_name varchar2(100),
  time timestamp(3),
  text varchar2(4000)
);

begin
  dbms_scheduler.create_job(
    job_name   => 'logger1',
    job_type   => 'PLSQL_BLOCK',
    job_action => 'BEGIN insert into scheduler_log values(''logger1'', systimestamp, ''Executed!''); commit; END;',
    start_date => systimestamp,
    repeat_interval => 'FREQ=SECONDLY; BYSECOND=30',
    end_date   => null,
    enabled    => true,
    comments   => 'Testing configuration');
end;
/

--
-- check how much work has been done and when
--

col job_name for a10
col time for a25
col text for a20

select * from scheduler_log order by time;

--
-- I want more job for my money !
--

update scheduler_config
   set repeat_interval = 'FREQ=SECONDLY; INTERVAL=10'
 where job_name = 'logger1';
commit;

--
-- remove the job
--

exec dbms_scheduler.drop_job('logger1')