Oracle 10g之前,可以使用dbms_job来管理定时任务。
10g之后,Oracle引入dbms_scheduler来替代先前的dbms_job,
在功能方面,它比dbms_job提供了更强大的功能和更灵活的机制/管理。
使用dbms_scheduler创建一个定时任务有两种形式
1)创建1个SCHEDULER来定义计划,1个PROGRAM来定义任务内容,
再创建1个JOB,为这个JOB指定上面的SCHEDULER和PROGRAM。
2)直接创建JOB,在参数里面直接指定计划和任务内容。
要执行DBMS_SCHEDULER需要有CREATE JOB权限。
如果要创建外部操作系统命令的job, 还必须有CREATE EXTERNAL JOB权限。
如果要对于某个对象进行操作,必须是它的owner,或者对它具有alter权限,或者有create any job权限。
如果要创建/操作job_class,还需要有MANAGE SCHEDULER权限。job_class都是创建在sys schema中的。
名称 | 类型 | 描述 |
CREATE_SCHEDULE | 过程 | DBMS_SCHEDULER.CREATE_SCHEDULE ( |
CREATE_PROGRAM | 过程 | DBMS_SCHEDULER.CREATE_PROGRAM ( program_name IN VARCHAR2, program_type IN VARCHAR2, program_action IN VARCHAR2, number_of_arguments IN PLS_INTEGER DEFAULT 0, enabled IN BOOLEAN DEFAULT FALSE, comments IN VARCHAR2 DEFAULT NULL); |
CREATE_JOB | 过程 | 通过事先创建的schedule object 和 program object来创建job: (另外还有2个过程只指定shcedule和program中的1个) DBMS_SCHEDULER.CREATE_JOB ( job_name IN VARCHAR2, program_name IN VARCHAR2, schedule_name IN VARCHAR2, job_class IN VARCHAR2 DEFAULT 'DEFAULT_JOB_CLASS', enabled IN BOOLEAN DEFAULT FALSE, auto_drop IN BOOLEAN DEFAULT TRUE, comments IN VARCHAR2 DEFAULT NULL, job_style IN VARCHAR2 DEFAULT 'REGULAR', credential_name IN VARCHAR2 DEFAULT NULL, destination_name IN VARCHAR2 DEFAULT NULL); DBMS_SCHEDULER.CREATE_JOB ( job_name IN VARCHAR2, job_type IN VARCHAR2, job_action IN VARCHAR2, number_of_arguments IN PLS_INTEGER DEFAULT 0, start_date IN TIMESTAMP WITH TIME ZONE DEFAULT NULL, repeat_interval IN VARCHAR2 DEFAULT NULL, end_date IN TIMESTAMP WITH TIME ZONE DEFAULT NULL, job_class IN VARCHAR2 DEFAULT 'DEFAULT_JOB_CLASS', enabled IN BOOLEAN DEFAULT FALSE, auto_drop IN BOOLEAN DEFAULT TRUE, comments IN VARCHAR2 DEFAULT NULL, credential_name IN VARCHAR2 DEFAULT NULL, destination_name IN VARCHAR2 DEFAULT NULL); |
SET_JOB_ARGUMENT_VALUE | 过程 | DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE ( DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE ( |
ENABLE DISABLE |
过程 | DBMS_SCHEDULER.ENABLE ( |
RUN_JOB STOP_JOB |
过程 | DBMS_SCHEDULER.RUN_JOB ( *use_current_session如果为TRUE: DBMS_SCHEDULER.STOP_JOB ( *force如果为FALSE, oracle尝试gracefully停止job, |
DROP_JOB DROP_PROGRAM DROP_SCHEDULE |
过程 | DBMS_SCHEDULER.DROP_JOB ( job_name IN VARCHAR2, force IN BOOLEAN DEFAULT FALSE, defer IN BOOLEAN DEFAULT FALSE, commit_semantics IN VARCHAR2 DEFAULT 'STOP_ON_FIRST_ERROR'); *如果force为TRUE,试图停止正在运行的job(调用STOP_JOB(force=>false)),之后再drop。 |
GET_ATTRIBUTE SET_ATTRIBUTE SET_ATTRIBUTE_NULL |
过程 | DBMS_SCHEDULER.GET_ATTRIBUTE ( DBMS_SCHEDULER.SET_ATTRIBUTE_NULL ( *一些可以设置的参数: max_failures max_runs restartable |
参数说明
1)schedule_name,program_name,job_name
这些名字在整个sql名字空间内必须唯一。比如,job_name不能和某个表名相同。
2)repeat_interval
定义job的计划,可以指定的形式相当丰富,也相当灵活,很强大。
regular_schedule = frequency_clause
[";" interval_clause] [";" bymonth_clause] [";" byweekno_clause]
[";" byyearday_clause] [";" bydate_clause] [";" bymonthday_clause]
[";" byday_clause] [";" byhour_clause] [";" byminute_clause]
[";" bysecond_clause] [";" bysetpos_clause] [";" include_clause]
[";" exclude_clause] [";" intersect_clause][";" periods_clause]
[";" byperiod_clause]
frequency_clause = "FREQ" "=" "YEARLY" | "MONTHLY" | "WEEKLY" | "DAILY" |
"HOURLY" | "MINUTELY" | "SECONDLY"
interval_clause = "INTERVAL" "=" 1 through 99
bymonth_clause = "BYMONTH" "=" {month ( "," month)*}
month = numeric_month | char_month
numeric_month = 1 | 2 | 3 ... 12
char_month = "JAN" | "FEB" | "MAR" | "APR" | "MAY" | "JUN" |
"JUL" | "AUG" | "SEP" | "OCT" | "NOV" | "DEC"
byweekno_clause = "BYWEEKNO" "=" {weeknumber ( "," weeknumber)*}
weeknumber = [minus] 1 through 53
byyearday_clause = "BYYEARDAY" "=" {yearday ( "," yearday)*}
yearday = [minus] 1 through 366
bydate_clause = "BYDATE" "=" {date ( "," date)*}
date = [YYYY]MMDD [ offset | span ]
bymonthday_clause = "BYMONTHDAY" "=" {monthday ( "," monthday)*}
monthday = [minus] 1 through 31
byday_clause = "BYDAY" "=" {byday ( "," byday)*}
byday = [weekdaynum] day
weekdaynum = [minus] daynum
daynum = 1 through 53 /* if frequency is yearly */
daynum = 1 through 5 /* if frequency is monthly */
day = "MON" | "TUE" | "WED" | "THU" | "FRI" | "SAT" | "SUN"
byhour_clause = "BYHOUR" "=" {hour ( "," hour)*}
hour = 0 through 23
byminute_clause = "BYMINUTE" "=" {minute ( "," minute)*}
minute = 0 through 59
bysecond_clause = "BYSECOND" "=" {second ( "," second)*}
second = 0 through 59
bysetpos_clause = "BYSETPOS" "=" {setpos ("," setpos)*}
setpos = [minus] 1 through 9999
include_clause = "INCLUDE" "=" schedule_list
exclude_clause = "EXCLUDE" "=" schedule_list
intersect_clause = "INTERSECT" "=" schedule_list
periods_clause = "PERIODS" "=" periodnum
byperiod_clause = "BYPERIOD" "=" {periodnum ("," periodnum)*}
periodnum = 1 through 100
offset = ("+" | "-") ["OFFSET:"] duration_val
span = ("+" | "-" | "^") "SPAN:" duration_val
duration_val = dur-weeks | dur_days
dur_weeks = numofweeks "W"
dur_days = numofdays "D"
numofweeks = 1 through 53
numofdays = 1 through 376
minus = "-"
举几个例子
a) 05/02 和 09/22 的 8:00 a.m., 1:00 p.m., 6:00 p.m.
'freq=daily;byhour=8,13,18;byminute=0;bysecond=0;bydate=0502,0922'
b) 每个月的最后一个工作日(注意,INTERVAL如果不指定,默认为1)
'FREQ=MONTHLY;BYDAY=MON,TUE,WED,THU,FRI;BYSETPOS=-1'
c) 1/10开始的5天,即1/10~1/14
BYDATE=0110+SPAN:5D
+表示从指定日期开始;-表示到指定日期结束;
^表示围绕指定日期的n天,如果n为偶数,调整为n+1。
d) 以下三种表示方法等同
BYDATE=0205-OFFSET:2W
BYDATE=0205-14D (the OFFSET: keyword is optional)
BYDATE=0122
3)start_date
实际上是schedule生效的日期,
对于一个定时执行的schedule,实际上的开始日期取决于repeat_interval参数。
如果start_date为null,等同于job被enable的时刻。
Oracle取start_date来作为repeat_interval的default值。
例如,如果指定start_date为1/31/2010 9:45:58,指定repeat_interval为'FREQ=YEARLY',
等同于'freq=yearly;bymonth=1;bymonthday=31;byhour=9;byminute=45;bysecond=58'
不能给repeat_interval指定时区信息,Oracle取start_date的时区信息作为指定的时区。
4)end_date
这个时间之后,job将不再被执行。
5)program_type/program_action/number_of_arguments/job_type/job_action
program_type有三种形式
-'PLSQL_BLOCK'
program_action是一个PL/SQL block. 不支持参数,number_of_arguments必须为0.
必须以分号结尾。例如以下三种形式
'my_proc();'
'BEGIN my_proc(); END;'
'DECLARE arg pls_integer:= 10; BEGIN my_proc2(arg); END;'
-'STORED_PROCEDURE'
program_action是一个存储过程,注意不支持INOUT or OUT参数,也不支持function.
参数最多255个,可以通过SET_JOB_ARGUMENT_VALUE过程来设定。
例如'"Schema"."Procedure"'.
-'EXECUTABLE'
program_action是一个外部操作系统的命令行程序,包含完整路径,不能带任何命令行参数。
开始的'?'会被替换为oracle home目录;'@'会被替换为当前oracle实例的SID。
job_type类似于program_type,只是多了'CHAIN'这种类型来创建任务链。
6) enabled
如果为TRUE, 创建时候检查program/job有效性,如果通过检查把program/job置为ENABLED.
在program/job使用前必须通过ENABLE过程把它置为ENABLED。
如果program为disabled,仅仅enable一个job,那么job会按计划执行,但是会失败。
7)auto_drop
如果为TRUE, 在job完成之后或者被自动disabled之后job将被自动删除。
如果满足以下条件,job被视为完成。
a) 过了end_date。
b) 达到了最大运行次数(max_runs,可以通过SET_ATTRIBUTE来指定)
c) 非重复任务,即只运行一次的任务,并且已经运行了1次。
如果满足以下条件,job被视为自动disabled.
a) 达到了最大运行失败次数(max_failures,可以通过SET_ATTRIBUTE来指定)
相关数据字典
1) dba_scheduler_jobs/user_scheduler_jobs
Enabling 一个 disabled 状态的job会重设本视图中的RUN_COUNT, FAILURE_COUNT and RETRY_COUNT.
2) dba_scheduler_programs/user_scheduler_programs
3) dba_scheduler_schedules/user_scheduler_schedules
4) dba_scheduler_job_log/dba_scheduler_job_run_details
5) dba_scheduler_job_classes
6) 默认情况下,数据库自身定义了一个PURGE_JOB的任务。dbms_scheduler提供了一个过程可以很方便的检验指定的计划(repeat_interval)。
DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING (
calendar_string IN VARCHAR2,
start_date IN TIMESTAMP WITH TIME ZONE,
return_date_after IN TIMESTAMP WITH TIME ZONE,
next_run_date OUT TIMESTAMP WITH TIME ZONE);
下面创建一个过程evaluate_repeat_interval,
传入repeat_interval字符串和计算的次数,会把每次的执行时间打印出来。
create or replace procedure evaluate_repeat_interval( p_repeat_interval in varchar2, p_number_of_evaluation in int, p_start_date in timestamp with time zone default sysdate ) as l_return_date_after timestamp with time zone; l_next_run_date timestamp with time zone; begin l_return_date_after := p_start_date; for i in 1..p_number_of_evaluation loop dbms_scheduler.evaluate_calendar_string( calendar_string=>p_repeat_interval, start_date=>p_start_date, return_date_after=>l_return_date_after, next_run_date=>l_next_run_date); dbms_output.put_line(l_next_run_date); l_return_date_after := l_next_run_date; end loop; end; --调用的例子1:每个月的最后一天 begin evaluate_repeat_interval( p_repeat_interval=>'FREQ=MONTHLY;BYMONTHDAY=-1', p_number_of_evaluation=>3, p_start_date=>to_timestamp_tz('2012-01-01 00:00:00.000 +08:00', 'yyyy-mm-dd hh24:mi:ss.ff tzh:tzm') ); end; --执行结果31-JAN-12 12.00.00.000000 AM +08:00 29-FEB-12 12.00.00.000000 AM +08:00 31-MAR-12 12.00.00.000000 AM +08:00
PL/SQL procedure successfully completed.
--调用的例子2:每个月的最后一个工作日 begin evaluate_repeat_interval( p_repeat_interval=>'FREQ=MONTHLY;BYDAY=MON,TUE,WED,THU,FRI;BYSETPOS=-1', p_number_of_evaluation=>3, p_start_date=>to_timestamp_tz('2012-01-01 00:00:00.000 +08:00', 'yyyy-mm-dd hh24:mi:ss.ff tzh:tzm') ); end; --执行结果31-JAN-12 12.00.00.000000 AM +08:00 29-FEB-12 12.00.00.000000 AM +08:00 30-MAR-12 12.00.00.000000 AM +08:00
PL/SQL procedure successfully completed.