1.通过DBMS_SCHEDULER.CREATE_JOB直接创建job
CREATE TABLE T_ROSANU ( R_ID VARCHAR2(10), R_DATE TIMESTAMP(6) ); --表已创建。 BEGIN DBMS_SCHEDULER.CREATE_JOB(JOB_NAME => 'job_create_rosanu', JOB_TYPE => 'PLSQL_BLOCK', JOB_ACTION => 'BEGIN INSERT INTO T_ROSANU VALUES ('' job '', SYSDATE); COMMIT; END;', ENABLED => TRUE, START_DATE => SYSTIMESTAMP, REPEAT_INTERVAL => 'SYSTIMESTAMP + 1/1440', COMMENTS => 'rosanu_create_job'); END; --PL/SQL 过程已成功完成。 SELECT R_ID, TO_CHAR(R_DATE, 'yyyy-mm-dd hh24:mi:ss') FROM T_ROSANU; /* R_ID TO_CHAR(R_DATE,'yyyy-mm-dd hh24:mi:ss) ----------------------------------------------- job 2013-03-29 18:12:11 job 2013-03-29 18:13:11 job 2013-03-29 18:14:11 */
这里的使用方法和dbms_jobs差不多,不过这个提供了加灵活的使用方法,比如可以执行匿名块和执行操作系统命令等;
2.CREATE_JOB结合CREATE_PROGRAM
CREATE OR REPLACE PROCEDURE PROC_ROSANU(IN_ID IN VARCHAR2) IS BEGIN INSERT INTO T_ROSANU VALUES (IN_ID, SYSDATE); COMMIT; END; -- 过程已创建。 -- 创建Program BEGIN DBMS_SCHEDULER.CREATE_PROGRAM(PROGRAM_NAME => 'program_rosanu', PROGRAM_ACTION => 'PROC_ROSANU', PROGRAM_TYPE => 'STORED_PROCEDURE', NUMBER_OF_ARGUMENTS => 1, COMMENTS => 'Rosanu_PROGRAM', ENABLED => FALSE); END; -- PL/SQL 过程已成功完成。 --设置Program参数 BEGIN DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(PROGRAM_NAME => 'program_rosanu', ARGUMENT_POSITION => 1, ARGUMENT_TYPE => 'VARCHAR2', DEFAULT_VALUE => 'program'); END; -- PL/SQL 过程已成功完成。 --执行Program EXEC DBMS_SCHEDULER.ENABLE('program_rosanu'); -- PL/SQL 过程已成功完成。 -- 创建Job BEGIN DBMS_SCHEDULER.CREATE_JOB(JOB_NAME => 'job_rosanu', PROGRAM_NAME => 'program_rosanu', COMMENTS => 'rosanu_create_job', REPEAT_INTERVAL => 'SYSTIMESTAMP + 1/1440', AUTO_DROP => FALSE, ENABLED => TRUE); END; -- PL/SQL 过程已成功完成。 SELECT R_ID, TO_CHAR(R_DATE, 'yyyy-mm-dd hh24:mi:ss') FROM T_ROSANU; /* R_ID TO_CHAR(R_DATE,'yyyy-mm-dd hh24:mi:ss') ------------------------------------------------ job 2013-02-29 20:20:11 program 2013-02-29 20:20:09 program 2013-02-29 20:21:09 job 2013-02-29 20:21:11 */
从这里使用的参数可以看出CREATE_PROGRAM把CREATE_JOB中的部分参数给独立出来,使得更加灵活的控制;
3.CREATE_JOB结合CREATE_PROGRAM和CREATE_SCHEDULE
exec DBMS_SCHEDULER.drop_job('job_create_rosanu'); --PL/SQL 过程已成功完成。 truncate table t_rosanu; --表被截断。 BEGIN DBMS_SCHEDULER.CREATE_SCHEDULE(REPEAT_INTERVAL => 'FREQ=MINUTELY;INTERVAL=1', START_DATE => SYSDATE, COMMENTS => 'rosanu_create_job', SCHEDULE_NAME => 'SCHEDULE_ROSANU'); END; -- PL/SQL 过程已成功完成。 BEGIN DBMS_SCHEDULER.CREATE_JOB(JOB_NAME => 't_xifenfei_job', PROGRAM_NAME => 'program_rosanu', COMMENTS => 'rosanu_create_job', SCHEDULE_NAME => 'SCHEDULE_ROSANU', AUTO_DROP => FALSE, ENABLED => TRUE); END; -- PL/SQL 过程已成功完成。 SELECT R_ID, TO_CHAR(R_DATE, 'yyyy-mm-dd hh24:mi:ss') FROM T_ROSANU; /* R_ID TO_CHAR(R_DATE,'yyyy-mm-dd hh24:mi:ss') ---------- -------------------------------------- job 2013-03-29 21:39:11 job 2013-03-29 21:37:11 job 2013-03-29 21:38:11 program 2013-03-29 21:39:01 program 2013-03-29 21:40:01 */
从这里可以看出CREATE_SCHEDULE把执行计划部分从CREATE_JOB中独立出来,使得控制力度更大,更加灵活;
补充说明:
1.还可以通过创建JOB_CLASS更加灵活的控制资源的使用情况,必须通过修改JOB_CLASS中的resource_consumer_group实现资源控制,service对应到数据库的service,可以实现rac中在哪个节点执行等等;
2.使用DBMS_SCHEDULER.set_attribute来修改相关属相如:
EXEC DBMS_SCHEDULER.set_attribute('GATHER_STATS_JOB','JOB_CLASS', 'AUTO_TASKS_JOB_CLASS2'); EXEC dbms_scheduler.set_attribute('WEEKNIGHT_WINDOW','REPEAT_INTERVAL','freq=daily;byday=MON,TUE,WED,THU,FRI;byhour=2;byminute=0;bysecond=0');
--DBMS_SCHEDULER 运行信息 SELECT JOB_NAME, STATE, ENABLED, TO_CHAR(LAST_START_DATE, 'yyyy-mm-dd hh24:mi:ss'), SCHEDULE_NAME FROM DBA_SCHEDULER_JOBS; --DBMS_SCHEDULER运行成功与否信息 SELECT LOG_ID, JOB_NAME, STATUS, TO_CHAR(ACTUAL_START_DATE, 'yyyy-mm-dd HH24:MI:ss') START_DATE, TO_CHAR(LOG_DATE, 'yyyy-mm-dd HH24:MI:ss') LOG_DATE FROM DBA_SCHEDULER_JOB_RUN_DETAILS WHERE JOB_NAME = 'JOB_ROSANU' ORDER BY 4 DESC; --查询执行时间情况 SELECT T1.WINDOW_NAME, T1.REPEAT_INTERVAL, T1.DURATION FROM DBA_SCHEDULER_WINDOWS T1, DBA_SCHEDULER_WINGROUP_MEMBERS T2 WHERE T1.WINDOW_NAME = T2.WINDOW_NAME AND T2.WINDOW_GROUP_NAME = 'MAINTENANCE_WINDOW_GROUP'; --修改执行时间 BEGINDBMS_SCHEDULER.SET_ATTRIBUTE('WEEKEND_WINDOW', 'REPEAT_INTERVAL', 'freq=daily;byday=SAT;byhour=0;byminute=0;bysecond=0'); DBMS_SCHEDULER.SET_ATTRIBUTE('WEEKEND_WINDOW', 'DURATION', '+002 00:00:00'); END;