支持的任务类型详解:
示例
手动执行计划
删除计划
Dbms_scheduler是Oracle提供创建计划任务的包,任务类型可以是执行PL\SQL程序、执行外部脚本、调用操作系统命令,通常用于创建定期定时的任务,不依赖操作系统,保存在数据库内,数据库迁移时不受影响,发生错误有日志可以查询,比较方便创建和使用。例如实施数据备份计划,将数据的备份的脚步保存在day_backup.sh 中,然后创建计划定期执行该脚步:
$ vim /oracle/db_backup.sh
#!/bin/sh
ORACLE_SID=sydb;
export ORACLE_SID
ORACLE_HOME=/u01/app/product/11.2.0/db_1;
export ORACLE_HOME
PATH=$ORACLE_HOME/bin:$PATH;
export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib;
export LD_LIBRARY_PATH
LOCAL=/EXPORT;
export LOCAL
$ORACLE_HOME/bin/rman target sys/oracle catalog rman/rman log='/tmp/db_backup.log' append <<EOF
run
{
allocate channel dev type disk;
backup incremental level 1 database plus archivelog format '/disk2/backup/sydb/%d_%U_%T.bkp'
tag 'db_backup';
release channel dev;
}
exit;
EOF
注意:这里要设置环境变量,在这里吃了好多苦,总之就是文件权限和环境变量要设置正确;
创建计划任务:
BEGIN
DBMS_SCHEDULER.CREATE_JOB(
job_name=>'DB_BACKUP', --job名称
job_type => 'EXECUTABLE', --job任务类型,请参考2中的解释
job_action =>'/oracle/db_backup.sh', --job操作
start_date=> to_date('2015-05-19 03:00:00','yyyy-mm-dd hh24:mi:ss'),--执行时间和执行间隔时间为null,则表示立即执行
/*12 天执行一次*/
repeat_interval=>'FREQ=MONTHLY;BYMONTHDAY=15,-1',--执行间隔时间
enabled=>true,--是否启用
/* next night at 11:00 PM */
comments => 'Nightly incremental backups');
END;
/
支持的任务类型详解:
job_type
This attribute specifies the type of job that you are creating. If it is not specified, an error is generated. The supported values are:
'PLSQL_BLOCK'
This specifies that the job is an anonymous PL/SQL block. Job or program arguments are not supported when the job or program type is PLSQL_BLOCK. In this case, the number of arguments must be 0.
'STORED_PROCEDURE'
This specifies that the job is a PL/SQL or Java stored procedure, or an external C subprogram. Only procedures, not functions with return values, are supported.
'EXECUTABLE'
This specifies that the job is external to the database. External jobs are anything that can be executed from the command line of the operating system. Anydata arguments are not supported with a job or program type of EXECUTABLE. The job owner must have the CREATE EXTERNAL JOB system privilege before the job can be enabled or run.
'CHAIN'
This specifies that the job is a chain. Arguments are not supported for a chain, so number_of_arguments must be 0.
示例
创建一张表,然后创建插入数据的过程:
create table t_insert(cname varchar2(30),cnum number(3,4);
/
create or replace procedure fun_insertdata
/*
功能:向t_insert 表中插入数据
edit:2015-05-15
*/
as
v_sql varchar2(300);
begin
v_sql:='insert into t_insert(cname,cnum)values(:1,:2)';
for i in 1..10000 loop
execute immediate v_sql using 'AAA',i;
end loop;
commit;
end;
/
最后创建一个立即执行的任务
BEGIN
DBMS_SCHEDULER.CREATE_JOB(
job_name=>'INSERT_DATATOTAB', --job名称
job_type => 'PLSQL_BLOCK', --job任务类型,请参考2中的解释
job_action =>'--job操作
BEGIN
fun_insertdata;
END;
',
start_date=>NULL, --执行时间和执行间隔时间如果为null,则表示立即执行
/*12 天执行一次*/
repeat_interval=>NULL,--执行间隔时间
enabled=>true,--是否启用
/* next night at 11:00 PM */
comments => 'Nightly incremental backups');
END;
/
查询结果:
SQL> column job_name format a15
SQL> column status format a8
SQL> column SESSION_ID format a7
SQL> column SLAVE_PID format a7
SQL> SELECT JOB_NAME,STATUS,session_id,slave_pid,cpu_used FROM DBA_SCHEDULER_JOB_RUN_DETAILS WHERE JOB_NAME='INSERT_DATATOTAB' and owner='SYS' ;
JOB_NAME STATUS SESSION SLAVE_P CPU_USED
--------------- -------- ------- ------- ---------------------------------------------------------------------------
INSERT_DATATOTA SUCCEEDE 173,263 5228 +000 00:00:01.36
B D 3
所以的scheduler 可以在dbms_schedulers 视图中查询到,每个scheduler 任务执行后的结果可以在DBA_SCHEDULER_JOB_RUN_DETAILS中查询到; 如果想定期执行job可以通过配置repeat_interval,比如每月1,5和最后一天可以这样配置
repeat_interval=>'FREQ=MONTHLY;BYMONTHDAY=1,5,-1'--执行间隔时间 ,-1表示每月最后一天
上面使用到一个FREQ=,这个表示执行的时间截: