--演示環境 scott@CNMMBO> select * from v$version where rownum<2; BANNER ---------------------------------------------------------------- Oracle Database 10g Release 10.2.0.3.0 - 64bit Production --建立演示表及序列 CREATE TABLE tb_schduler ( id NUMBER (10) NOT NULL, descr VARCHAR2 (20) NOT NULL, cr_date DATE NOT NULL, CONSTRAINT tb_schduler_pk PRIMARY KEY (id) ); CREATE SEQUENCE tb_schduler_seq; 1、建立程序 --下面定義了3個須要用到的程序program,注意這裏的program不等同於procedure或者package,可是能夠調用procedure或package --下面的program主要是用於插入記錄到測試表 BEGIN DBMS_SCHEDULER.create_program ( program_name => 'test_proc_1', program_type => 'PLSQL_BLOCK', -->這裏的類型定義爲PLSQL_BLOCK,支持STORED PROCEDURE/EXECUTEABLE program_action => 'BEGIN INSERT INTO tb_schduler (id, descr, cr_date) VALUES (tb_schduler_seq.NEXTVAL, ''test_proc_1'', SYSDATE); COMMIT; END;', enabled => TRUE, comments => 'Program for first link in the chain.'); DBMS_SCHEDULER.create_program ( program_name => 'test_proc_2', program_type => 'PLSQL_BLOCK', program_action => 'BEGIN INSERT INTO tb_schduler (id, descr, cr_date) VALUES (tb_schduler_seq.NEXTVAL, ''test_proc_2'', SYSDATE); COMMIT; END;', enabled => TRUE, comments => 'Program for second link in the chain.'); DBMS_SCHEDULER.create_program ( program_name => 'test_proc_3', program_type => 'PLSQL_BLOCK', program_action => 'BEGIN INSERT INTO tb_schduler (id, descr, cr_date) VALUES (tb_schduler_seq.NEXTVAL, ''test_proc_3'', SYSDATE); COMMIT; END;', enabled => TRUE, comments => 'Program for last link in the chain.'); END; / 2、建立chain --建立chain比較簡單,一般只須要定義一個chain名字便可,主要是用於關聯後續定義rule及step BEGIN DBMS_SCHEDULER.create_chain ( chain_name => 'test_chain_1', -->定義chain的名字 rule_set_name => NULL, -->能夠指定規則集的名字 evaluation_interval => NULL, comments => 'A test chain.'); END; / 3、定義chain步驟 --下面定義chain的每個步驟以及其對應的program_name,也就是每一步須要作什麼 BEGIN DBMS_SCHEDULER.define_chain_step ( chain_name => 'test_chain_1', --->chain的名字 step_name => 'chain_step_1', --->步驟地名字 program_name => 'test_proc_1'); --->當前步驟應執行的相應程序 DBMS_SCHEDULER.define_chain_step ( chain_name => 'test_chain_1', step_name => 'chain_step_2', program_name => 'test_proc_2'); DBMS_SCHEDULER.define_chain_step ( chain_name => 'test_chain_1', step_name => 'chain_step_3', program_name => 'test_proc_3'); END; / 4、定義chain規則 --用於定義chain根據執行結果應該如何跳轉的問題,每一個CHAIN 規則都擁有condition和action 屬性, --當知足condition 時則執行action中指定的step。使用DBMS_SCHEDULER.DEFINE_CHAIN_RULE 過程 BEGIN DBMS_SCHEDULER.define_chain_rule ( chain_name => 'test_chain_1', condition => 'TRUE', action => 'START "CHAIN_STEP_1"', rule_name => 'chain_rule_1', comments => 'First link in the chain.'); DBMS_SCHEDULER.define_chain_rule ( chain_name => 'test_chain_1', condition => '"CHAIN_STEP_1" COMPLETED', action => 'START "CHAIN_STEP_2"', rule_name => 'chain_rule_2', comments => 'Second link in the chain.'); DBMS_SCHEDULER.define_chain_rule ( chain_name => 'test_chain_1', condition => '"CHAIN_STEP_2" COMPLETED', action => 'START "CHAIN_STEP_3"', rule_name => 'chain_rule_3', comments => 'Third link in the chain.'); DBMS_SCHEDULER.define_chain_rule ( chain_name => 'test_chain_1', condition => '"CHAIN_STEP_3" COMPLETED', action => 'END', rule_name => 'chain_rule_4', comments => 'End of the chain.'); END; / 5、激活chain BEGIN DBMS_SCHEDULER.enable ('test_chain_1'); END; / 6、將chain添加到job BEGIN DBMS_SCHEDULER.CREATE_JOB ( job_name => 'test_chain_1_job', job_type => 'CHAIN', job_action => 'test_chain_1', repeat_interval => 'freq=minutely; interval=2', start_date => SYSTIMESTAMP, end_date => SYSTIMESTAMP + (1/48), enabled => FALSE); --->值爲TRUE用於激活JOB END; / 7、手動執行chain BEGIN DBMS_SCHEDULER.run_chain ( chain_name => 'test_chain_1', job_name => 'test_chain_1_run_job', start_steps => 'chain_step_1,chain_step_3'); -->能夠指定單步或多步以及全部步驟 END; / scott@CNMMBO> select * from tb_schduler; ID DESCR CR_DATE ---------- -------------------- ----------------- 1 test_proc_1 20131203 14:36:03 2 test_proc_3 20131203 14:36:04 --激活job scott@CNMMBO> exec dbms_scheduler.enable('test_chain_1_job'); PL/SQL procedure successfully completed.
參考自:https://blog.csdn.net/leshami/article/details/17096009測試