[oracle@tyger dbs]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Tue May 6 13:02:41 2014
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
/* 創建測試用戶tyger 賦予權限 */
SYS@ORCL>drop user tyger cascade;
User dropped.
SYS@ORCL>create user tyger identified by tyger account unlock;
User created.
SYS@ORCL>grant connect,resource to tyger;
Grant succeeded.
SYS@ORCL>conn tyger/tyger
Connected.
/* 創建序列 seq_chain 以及測試表 tab1 tab2 tab3 */
TYGER@ORCL>create sequence seq_chain;
Sequence created.
TYGER@ORCL>create table tab1(id number(5),cdate date,sdate date default sysdate);
Table created.
TYGER@ORCL>create table tab2(id number(5),cdate date,sdate date default sysdate);
Table created.
TYGER@ORCL>create table tab3(id number(5),cdate date,sdate date default sysdate);
Table created.
/* 創建program 報錯權限不足 */
TYGER@ORCL>begin
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name => 'CHAIN_P1',
program_type => 'PLSQL_BLOCK',
program_action => 'begin
execute immediate ''insert into tab1(id,cdate)
values(seq_chain.nextval,add_months(sysdate,seq_chain.currval))'';
end;',
enabled => true);
end;
/
2 3 4 5 6 7 8 9 10 11
*
ERROR at line 1:
ORA-27486: insufficient privileges
ORA-06512: at "SYS.DBMS_ISCHED", line 5
ORA-06512: at "SYS.DBMS_SCHEDULER", line 30
ORA-06512: at line 2
/* 賦予必要權限 以及接下去實驗需要的權限 */
TYGER@ORCL>conn / as sysdba
Connected.
SYS@ORCL>grant create job to tyger;
Grant succeeded.
SYS@ORCL>grant create evaluation context to tyger;
Grant succeeded.
SYS@ORCL>grant create rule set to tyger;
Grant succeeded.
SYS@ORCL>grant create rule to tyger;
Grant succeeded.
/* 創建program */
SYS@ORCL>conn tyger/tyger
Connected.
TYGER@ORCL>begin
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name => 'CHAIN_P1',
program_type => 'PLSQL_BLOCK',
program_action => 'begin
execute immediate ''insert into tab1(id,cdate)
values(seq_chain.nextval,add_months(sysdate,seq_chain.currval))'';
end;',
enabled => true);
end;
/
2 3 4 5 6 7 8 9 10 11
PL/SQL procedure successfully completed.
TYGER@ORCL>begin
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name => 'chain_step2',
program_type => 'PLSQL_BLOCK',
program_action => 'begin
execute immediate ''insert into tab2(id,cdate)
values(seq_chain.nextval,add_months(sysdate,seq_chain.currval))'';
end;',
enabled => true);
end;
/
2 3 4 5 6 7 8 9 10 11
PL/SQL procedure successfully completed.
TYGER@ORCL>begin
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name => 'CHAIN_P3',
program_type => 'PLSQL_BLOCK',
program_action => 'begin
execute immediate ''insert into tab3(id,cdate)
values(seq_chain.nextval,add_months(sysdate,seq_chain.currval))'';
end;',
enabled => true);
end;
/
2 3 4 5 6 7 8 9 10 11
PL/SQL procedure successfully completed.
/* 創建鏈tyger_chain */
TYGER@ORCL>exec dbms_scheduler.create_chain('tyger_chain');
PL/SQL procedure successfully completed.
TYGER@ORCL>select chain_name from user_scheduler_chains;
CHAIN_NAME
------------------------------
TYGER_CHAIN
/* 創建chain step
為鏈TYGER_CHAIN 添加step ,可以一個一個的添加,也可以一下全部添加 */
TYGER@ORCL>
TYGER@ORCL>begin
dbms_scheduler.define_chain_step(
chain_name =>'TYGER_CHAIN',
step_name =>'CHAIN_STEP1',
program_name => 'CHAIN_P1');
end;
/
2 3 4 5 6 7
PL/SQL procedure successfully completed.
TYGER@ORCL>begin
dbms_scheduler.define_chain_step(
chain_name =>'TYGER_CHAIN',
step_name =>'CHAIN_STEP2',
program_name => 'chain_step2');
dbms_scheduler.define_chain_step(
chain_name =>'TYGER_CHAIN',
step_name =>'chain_step3',
program_name => 'CHAIN_P3');
end;
/
2 3 4 5 6 7 8 9 10 11
PL/SQL procedure successfully completed.
/* 查看創建的step 是否成功 */
TYGER@ORCL>col chain_name for a16
TYGER@ORCL>col step_name for a16
TYGER@ORCL>col program_name for a16
TYGER@ORCL>select chain_name,step_name,program_name
2 from user_scheduler_chain_steps;
CHAIN_NAME STEP_NAME PROGRAM_NAME
---------------- ---------------- ----------------
TYGER_CHAIN CHAIN_STEP1 CHAIN_P1
TYGER_CHAIN CHAIN_STEP2 CHAIN_P2
TYGER_CHAIN CHAIN_STEP3 CHAIN_P3
/* 為鏈創建規則 */
/* 規則:首先執行 chain_step1,成功完成後執行 chain_step2 ,成功完成後執行 chain_step3,成功完成後,結束。 */ TYGER@ORCL>
TYGER@ORCL>begin
DBMS_SCHEDULER.DEFINE_CHAIN_RULE (
chain_name => 'TYGER_CHAIN',
condition => 'true',
action => 'start CHAIN_STEP1',
rule_name => 'rule_01');
DBMS_SCHEDULER.DEFINE_CHAIN_RULE (
chain_name => 'TYGER_CHAIN',
condition => 'CHAIN_STEP1 SUCCEEDED',
action => 'start chain_step2',
rule_name => 'rule_02');
DBMS_SCHEDULER.DEFINE_CHAIN_RULE (
chain_name => 'TYGER_CHAIN',
condition => 'chain_step2 SUCCEEDED',
action => 'start chain_step3',
rule_name => 'rule_03');
DBMS_SCHEDULER.DEFINE_CHAIN_RULE (
chain_name => 'TYGER_CHAIN',
condition => 'chain_step3 SUCCEEDED',
action => 'END 0',
rule_name => 'rule_04');
END;
/
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
PL/SQL procedure successfully completed.
/* 啟用鏈 tyger_chain */
TYGER@ORCL>exec dbms_scheduler.enable('TYGER_CHAIN');
PL/SQL procedure successfully completed.
手動運行鏈 tyger_chain
TYGER@ORCL>begin
DBMS_SCHEDULER.RUN_CHAIN (
chain_name =>'TYGER_CHAIN',
start_steps =>'CHAIN_STEP1');
end;
/
2 3 4 5 6
PL/SQL procedure successfully completed.
/* 驗證
是不是按照我們設計的規則 tab1 -> tab2 -> tab3 ->exit 完成的。 */
TYGER@ORCL>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
TYGER@ORCL>select * from tab1;
ID CDATE SDATE
---------- ------------------- -------------------
1 2014-
06-06 13:14:24 2014-05-06 13:14:24
TYGER@ORCL>select * from tab2;
ID CDATE SDATE
---------- ------------------- -------------------
2 2014-
07-06 13:14:25 2014-05-06 13:14:25
TYGER@ORCL>select * from tab3;
ID CDATE SDATE
---------- ------------------- -------------------
3 2014-
08-06 13:14:25 2014-05-06 13:14:25
TYGER@ORCL>