需求:每晚有先后顺序的执行10个存储。
数据库: 11gR2
原本以为是个easy的问题,用job就能解决了。于是找到文档上的job使用方法,没曾想:
The DBMS_JOB
package has been superseded by theDBMS_SCHEDULER
package.
好吧,又来学习一下 DBMS_SCHEDULER包!
花了2天时间来实现该需求,现将学习dbms_scheduler包的使用方法做下记录,由于该包的内容太多,我只把解决实际问题所用到的知识列出:
我的学习思路:
1:弄清概念
2:试验
3:参照官方文档做博客
begin:
一、概念
job:oracle中 个人理解为:任务
program:个人理解为 :需要被执行的程序(plsql代码,os脚本,存储)
scheduler:个人理解为:定义执行的周期或者频率
君三思的解释是: program 负责做什么,schedule 负责啥时候做,job 就简单了,一个字:做!!
二、试验
1:创建一个表:
create table t(id number(20),time_in date);
2:创建一个存储过程:
create or replace procedure insert_t
as
begin
insert into t values(t_seq.nextval,sysdate);
end;
/
3:创建一个程序块:
begin
DBMS_SCHEDULER.CREATE_PROGRAM(
program_name => 'my_t_pro',
program_type => 'STORED_PROCEDURE',
program_action => 't_t.insert_t',
enabled => true,
comments => 'this is a program!'
);
end;
/
DBMS_SCHEDULER.CREATE_PROGRAM的使用方法参考:
4:创建一个schedule:
begin
DBMS_SCHEDULER.CREATE_SCHEDULE(
schedule_name => 'my_t_scheduler',
start_date => sysdate,
repeat_interval => 'FREQ=MINUTELY;INTERVAL=1',
comments => 'every 2 minute'
);
end;
/
DBMS_SCHEDULER.CREATE_SCHEDULE的使用方法参考:
5:创建一个 job
begin
dbms_scheduler.create_job(
job_name => 'exec_my_t_pro',
program_name => 'my_t_pro',
schedule_name => 'my_t_scheduler',
enabled => true
);
end;
/
dbms_scheduler.create_job的使用方法参考:
使用参考整理自三思笔记
创建完成后 oracle就自动开始执行刚刚定义的job了
SQL> select * from t;
ID TIME_IN
---------------- --------------------------------------------------------------------------------
2 2013/3/13 7:26:22
3 2013/3/13 7:27:21
要查看当前用户的job相关信息 : user_scheduler_job_run_details
后续将整理几个例子方便以后参考!!
闲了几天,今天在回头来看整理的内容发现还不能完成 需求:每晚有先后顺序的执行10个存储。
没法还得继续学习。
下面是实现该需求的测试代码,运用到了oracle scheduler中的chain。过程如下:
1:创建2个表
create table t (id number(10),f_time date);
create table t1(id number(10),name varchar2(20));
create sequence t_seq INCREMENT BY 1 START WITH 1 noMAXVALUE NOCYCLE CACHE 20;
2:创建2个存储:
create or replace procedure in_t
as
begin
insert into t values(t_seq.nextval,sysdate);
commit;
end;
/
create or replace procedure in_t1
as
m number(10);
begin
m := t_seq.nextval;
insert into t1 values(m,'name'||m);
commit;
end;
/
3:创建program 以下脚本在命令行完成
begin
DBMS_SCHEDULER.CREATE_PROGRAM(
program_name => 'my_t_pro',
program_type => 'STORED_PROCEDURE',
program_action => 't.in_t',
enabled => true,
comments => 'this is a program!'
);
end;
/
begin
DBMS_SCHEDULER.CREATE_PROGRAM(
program_name => 'my_t1_pro',
program_type => 'STORED_PROCEDURE',
program_action => 't.in_t1',
enabled => true,
comments => 'this is a program!'
);
end;
/
4:创建链(chain),chain的概念和使用方法在:参考整理自官方文档
exec dbms_scheduler.create_chain('t_chain');
5:创建连的步骤(step)
begin
DBMS_SCHEDULER.DEFINE_CHAIN_STEP (
chain_name => 't_chain',
step_name => 's_1',
program_name => 'my_t_pro'
);
DBMS_SCHEDULER.DEFINE_CHAIN_STEP (
chain_name => 't_chain',
step_name => 's_2',
program_name => 'my_t1_pro'
);
end;
/
6:创建链的规则(rule)
begin
DBMS_SCHEDULER.DEFINE_CHAIN_RULE(
chain_name => 't_chain',
condition => 'true',
action => 'start s_1',
rule_name => 'r1'
);
DBMS_SCHEDULER.DEFINE_CHAIN_RULE(
chain_name => 't_chain',
condition => 's_1 completed',
action => 'start s_2',
rule_name => 'r2'
);
DBMS_SCHEDULER.DEFINE_CHAIN_RULE (
chain_name => 't_chain',
condition => 's_2 completed',
action => 'end 0',
rule_name => 'r3'
);
end;
/
一口气做了这么多,还不明白是干了些什么?下面粗略解释以下:
1-3不解释!
步骤4=> 创建链(chain):chain就是2个或者2个以上的program按照一定的步骤(setp !如:先执行my_t_pro,后执行my_t1_pro)并在一定的规则(rule!例:如果my_t_pro执行成功后,才执行my_t1_pro,执行完成后关闭chain)下有序运行的逻辑关系称为链。该步骤创建了一个名为 t_chain的链(chain)
步骤5=>给步骤4创建的链,指定执行的顺序。
步骤6=>给步骤4创建的链,指定怎么执行的规则。
创建完成后就可以手动的测试了,注意在测试前要启用链(chain):
exec DBMS_SCHEDULER.ENABLE('t_chain');
A chain is always created as disabled and must be enabled with theENABLE Procedure before it can be used.
7:手动测试
BEGIN
DBMS_SCHEDULER.RUN_CHAIN (
CHAIN_NAME => 't_chain',
START_STEPS => 's_1'
);
END;
/
以下是测试结果:
SQL> BEGIN
2 DBMS_SCHEDULER.RUN_CHAIN (
3 CHAIN_NAME => 't_chain',
4 START_STEPS => 's_1'
5 );
6 END;
7 /
PL/SQL procedure successfully completed.
SQL> select * from t;
ID F_TIME
---------- ------------
1 21-MAR-13
SQL> select * from t1;
ID NAME
---------- --------------------
2 name2