2013-08-21 35 views
0

我正在使用確定的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

嘗試移除最後分號 – OldProgrammer

回答

0

我想這個問題是該屬性值包含分號「;」因爲如果我不使用它們,我不會收到錯誤。

你有任何建議來規避這個問題嗎?

Err ...你的問題沒有意義。你知道這個問題,但你不想修復你在repeat_intervalcalendaring syntax的語法錯誤?

對於這個簡單的例子,你的觸發器看起來不太複雜(但你可能有一個合理的理由使用DBMS_JOB雖然)。下面是首先設置一個預定作業以在第30秒,每分鐘運行,並且再後來經由配置表改變repeat_interval到每10秒運行的例子:後「INTERVAL = 1」

-- 
-- 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') 
+0

謝謝,這個例子太棒了! –