8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » 10g » Here

Scheduler (DBMS_SCHEDULER) in Oracle Database 10g Onward

Oracle 10g introduced a comprehensive scheduler (DBMS_SCHEDULER) to replace and extend the functionality provided by the DBMS_JOB package. Jobs form the core of the functionality, but there are several other components available.

This is a server-based scheduler, so everything is done in the context of the database server. It is nothing to do with scheduling things to happen on a client PC.

Related articles.

Simple Example

Although the scheduler is capable of very complicated schedules, on many occasions you just want to create a simple job with everything defined inline. If that's all you want, the following example is for you.

BEGIN
  DBMS_SCHEDULER.create_job (
    job_name        => 'test_full_job_definition',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN my_job_procedure; END;',
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=hourly; byminute=0; bysecond=0;',
    enabled         => TRUE);
END;
/

Programs

The scheduler allows you to optionally create programs which hold metadata about a task, but no schedule information. A program may related to a PL/SQL block, a stored procedure or an OS executable file. Programs are created using the CREATE_PROGRAM procedure.

-- Create the test programs.
BEGIN
  -- PL/SQL Block.
  DBMS_SCHEDULER.create_program (
    program_name   => 'test_plsql_block_prog',
    program_type   => 'PLSQL_BLOCK',
    program_action => 'BEGIN DBMS_STATS.gather_schema_stats(''SCOTT''); END;',
    enabled        => TRUE,
    comments       => 'Program to gather SCOTT''s statistics using a PL/SQL block.');

  -- Shell Script.
  DBMS_SCHEDULER.create_program (
    program_name        => 'test_executable_prog',
    program_type        => 'EXECUTABLE',
    program_action      => '/u01/app/oracle/dba/gather_scott_stats.sh',
    number_of_arguments => 0,
    enabled             => TRUE,
    comments            => 'Program to gather SCOTT''s statistics us a shell script.');

  -- Stored Procedure with Arguments.
  DBMS_SCHEDULER.create_program (
    program_name        => 'test_stored_procedure_prog',
    program_type        => 'STORED_PROCEDURE',
    program_action      => 'DBMS_STATS.gather_schema_stats',
    number_of_arguments => 1,
    enabled             => FALSE,
    comments            => 'Program to gather SCOTT''s statistics using a stored procedure.');

  DBMS_SCHEDULER.define_program_argument (
    program_name      => 'test_stored_procedure_prog',
    argument_name     => 'ownname',
    argument_position => 1,
    argument_type     => 'VARCHAR2',
    default_value     => 'SCOTT');

  DBMS_SCHEDULER.enable (name => 'test_stored_procedure_prog');
END;
/

PL/SQL procedure successfully completed.

-- Display the program details.
SELECT owner, program_name, enabled FROM dba_scheduler_programs;

OWNER                          PROGRAM_NAME                   ENABL
------------------------------ ------------------------------ -----
SYS                            PURGE_LOG_PROG                 TRUE
SYS                            GATHER_STATS_PROG              TRUE
SYS                            TEST_PLSQL_BLOCK_PROG          TRUE
SYS                            TEST_EXECUTABLE_PROG           TRUE
SYS                            TEST_STORED_PROCEDURE_PROG     TRUE

5 rows selected.

Notice how programs that accept arguments must have the arguments defined before they can be enabled.

Programs can be deleted using the DROP_PROGRAM procedure.

BEGIN
  DBMS_SCHEDULER.drop_program (program_name => 'test_plsql_block_prog');
  DBMS_SCHEDULER.drop_program (program_name => 'test_stored_procedure_prog');
  DBMS_SCHEDULER.drop_program (program_name => 'test_executable_prog');
END;
/

PL/SQL procedure successfully completed.

-- Display the program details.
SELECT owner, program_name, enabled FROM dba_scheduler_programs;

OWNER                          PROGRAM_NAME                   ENABL
------------------------------ ------------------------------ -----
SYS                            PURGE_LOG_PROG                 TRUE
SYS                            GATHER_STATS_PROG              TRUE

2 rows selected.

Schedules

Schedules optionally define the start time, end time and interval related to a job. Schedules are created using the CREATE_SCHEDULE procedure.

-- Create the schedule.
BEGIN
  DBMS_SCHEDULER.create_schedule (
    schedule_name   => 'test_hourly_schedule',
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=hourly; byminute=0',
    end_date        => NULL,
    comments        => 'Repeats hourly, on the hour, for ever.');
END;
/

PL/SQL procedure successfully completed.

-- Display the schedule details.
SELECT owner, schedule_name FROM dba_scheduler_schedules;

OWNER                          SCHEDULE_NAME
------------------------------ ------------------------------
SYS                            DAILY_PURGE_SCHEDULE
SYS                            TEST_HOURLY_SCHEDULE

2 rows selected.

Notice how the interval is defined using the calendaring syntax. This is explained below.

A schedule can be dropped using the DROP_SCHEDULE procedure.

BEGIN
  DBMS_SCHEDULER.drop_schedule (schedule_name => 'TEST_HOURLY_SCHEDULE');
END;
/

PL/SQL procedure successfully completed.

-- Display the schedule details.
SELECT owner, schedule_name FROM dba_scheduler_schedules;

OWNER                          SCHEDULE_NAME
------------------------------ ------------------------------
SYS                            DAILY_PURGE_SCHEDULE

1 row selected.

Schedules don't have to be created as separate objects. They can be defined using the REPEAT_INTERVAL parameter of the CREATE_JOB procedure.

Jobs

Jobs are what the scheduler is all about. They can either be made up of predefined parts (programs and schedules) or completely self contained depending on which overload of the CREATE_JOB procedure is used to create them.

-- Create jobs.
BEGIN
  -- Job defined entirely by the CREATE JOB procedure.
  DBMS_SCHEDULER.create_job (
    job_name        => 'test_full_job_definition',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN DBMS_STATS.gather_schema_stats(''SCOTT''); END;',
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=hourly; byminute=0',
    end_date        => NULL,
    enabled         => TRUE,
    comments        => 'Job defined entirely by the CREATE JOB procedure.');

  -- Job defined by an existing program and schedule.
  DBMS_SCHEDULER.create_job (
    job_name      => 'test_prog_sched_job_definition',
    program_name  => 'test_plsql_block_prog',
    schedule_name => 'test_hourly_schedule',
    enabled       => TRUE,
    comments      => 'Job defined by an existing program and schedule.');

  -- Job defined by existing program and inline schedule.
  DBMS_SCHEDULER.create_job (
    job_name        => 'test_prog_job_definition',
    program_name    => 'test_plsql_block_prog',
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=hourly; byminute=0',
    end_date        => NULL,
    enabled         => TRUE,
    comments        => 'Job defined by existing program and inline schedule.');

  -- Job defined by existing schedule and inline program.
  DBMS_SCHEDULER.create_job (
     job_name      => 'test_sched_job_definition',
     schedule_name => 'test_hourly_schedule',
     job_type      => 'PLSQL_BLOCK',
     job_action    => 'BEGIN DBMS_STATS.gather_schema_stats(''SCOTT''); END;',
     enabled       => TRUE,
     comments      => 'Job defined by existing schedule and inline program.');
END;
/

PL/SQL procedure successfully completed.

-- Display job details.
SELECT owner, job_name, enabled FROM dba_scheduler_jobs;

OWNER                          JOB_NAME                       ENABL
------------------------------ ------------------------------ -----
SYS                            PURGE_LOG                      TRUE
SYS                            GATHER_STATS_JOB               TRUE
SYS                            TEST_FULL_JOB_DEFINITION       TRUE
SYS                            TEST_PROG_SCHED_JOB_DEFINITION TRUE
SYS                            TEST_PROG_JOB_DEFINITION       TRUE
SYS                            TEST_SCHED_JOB_DEFINITION      TRUE

6 rows selected.

Jobs are normally run asynchronously under the control of the job coordinator, but they can be controlled manually using the RUN_JOB and STOP_JOB procedures.

BEGIN
  -- Run job synchronously.
  DBMS_SCHEDULER.run_job (job_name            => 'test_full_job_definition',
                          use_current_session => TRUE);

  -- Stop jobs.
  DBMS_SCHEDULER.stop_job (job_name => 'test_full_job_definition, test_prog_sched_job_definition');
END;
/

Jobs can be deleted using the DROP_JOB procedure.

BEGIN
  DBMS_SCHEDULER.drop_job (job_name => 'test_full_job_definition');
  DBMS_SCHEDULER.drop_job (job_name => 'test_prog_sched_job_definition');
  DBMS_SCHEDULER.drop_job (job_name => 'test_prog_job_definition');
  DBMS_SCHEDULER.drop_job (job_name => 'test_sched_job_definition');
END;
/

PL/SQL procedure successfully completed.

-- Display job details.
SELECT owner, job_name, enabled FROM dba_scheduler_jobs;

OWNER                          JOB_NAME                       ENABL
------------------------------ ------------------------------ -----
SYS                            PURGE_LOG                      TRUE
SYS                            GATHER_STATS_JOB               TRUE

2 rows selected.

Job Classes

Job classes allow grouping of jobs with similar characteristics and resource requierments which eases administration. If the JOB_CLASS parameter of the CREATE_JOB procedure is undefined the job is assigned to the DEFAULT_JOB_CLASS. A job class is created using the CREATE_JOB_CLASS procedure.

-- Display the current resource consumer groups.
SELECT consumer_group FROM dba_rsrc_consumer_groups;

CONSUMER_GROUP
------------------------------
OTHER_GROUPS
DEFAULT_CONSUMER_GROUP
SYS_GROUP
LOW_GROUP
AUTO_TASK_CONSUMER_GROUP

5 rows selected.

-- Create a job class.
BEGIN
  DBMS_SCHEDULER.create_job_class (
    job_class_name          =>  'test_job_class',
    resource_consumer_group =>  'low_group');
END;
/

PL/SQL procedure successfully completed.

-- Display job class details.
SELECT job_class_name, resource_consumer_group FROM dba_scheduler_job_classes;

JOB_CLASS_NAME                 RESOURCE_CONSUMER_GROUP
------------------------------ ------------------------------
DEFAULT_JOB_CLASS
AUTO_TASKS_JOB_CLASS           AUTO_TASK_CONSUMER_GROUP
TEST_JOB_CLASS                 LOW_GROUP

3 rows selected.

Jobs can be assigned to a job class either during or after creation using the SET_ATTRIBUTE procedure.

BEGIN
  -- Job defined by an existing program and schedule and assigned to a job class.
  DBMS_SCHEDULER.create_job (
    job_name      => 'test_prog_sched_class_job_def',
    program_name  => 'test_plsql_block_prog',
    schedule_name => 'test_hourly_schedule',
    job_class     => 'test_job_class',
    enabled       => TRUE,
    comments      => 'Job defined by an existing program and schedule and assigned toa job class.');

  DBMS_SCHEDULER.set_attribute (
    name      => 'test_prog_sched_job_definition',
    attribute => 'job_class',
    value     => 'test_job_class');
END;
/

PL/SQL procedure successfully completed.

-- Display job details.
SELECT owner, job_name, job_class, enabled FROM dba_scheduler_jobs;

OWNER                          JOB_NAME                       JOB_CLASS                      ENABL
------------------------------ ------------------------------ ------------------------------ -----
SYS                            PURGE_LOG                      DEFAULT_JOB_CLASS              TRUE
SYS                            GATHER_STATS_JOB               AUTO_TASKS_JOB_CLASS           TRUE
SYS                            TEST_FULL_JOB_DEFINITION       DEFAULT_JOB_CLASS              TRUE
SYS                            TEST_PROG_SCHED_JOB_DEFINITION TEST_JOB_CLASS                 TRUE
SYS                            TEST_PROG_JOB_DEFINITION       DEFAULT_JOB_CLASS              TRUE
SYS                            TEST_SCHED_JOB_DEFINITION      DEFAULT_JOB_CLASS              TRUE
SYS                            TEST_PROG_SCHED_CLASS_JOB_DEF  TEST_JOB_CLASS                 TRUE

7 rows selected.

Job classes can be dropped using DROP_JOB_CLASS procedure.

BEGIN
  DBMS_SCHEDULER.drop_job_class (
    job_class_name => 'test_job_class',
    force          => TRUE);
END;
/

PL/SQL procedure successfully completed.

-- Display job class details.
SELECT job_class_name, resource_consumer_group FROM dba_scheduler_job_classes;

JOB_CLASS_NAME                 RESOURCE_CONSUMER_GROUP
------------------------------ ------------------------------
DEFAULT_JOB_CLASS
AUTO_TASKS_JOB_CLASS           AUTO_TASK_CONSUMER_GROUP

2 rows selected.

The force option disables any dependent jobs and sets their job class to the default value. If the job class has no dependents the force option is not necessary.

Jobs can be assigned a priority between 1 and 5, which 1 being the highest, which the job queue coordinator can use to prioritise jobs within their class. If no priority is assigned, a priority of 3 us used.

BEING
  DBMS_SCHEDULER.set_attribute (
    name      => 'my_job', 
    attribute => 'job_priority',
    value     => 1);
END;
/

Windows

Windows provide the link between the scheduler and the resource manager, allowing different resource plans to be activated at different times. Since job classes point to resource consumer groups, and therefore resource plans, this mechanism allows control over the resources allocated to job classes and their jobs during specific time periods.

Only one window can be active (open) at any time, with one resource plan assigned to the window. The affect of resource plan switches is instantly visible to running jobs which are assigned to job classes. The interaction between the resource manager and the scheduler is beyond the scope of this document.

A window can be created using the CREATE_WINDOW procedure with a predefined or an inline schedule.

BEGIN
  -- Window with a predefined schedule.
  DBMS_SCHEDULER.create_window (
    window_name     => 'test_window_1',
    resource_plan   => NULL,
    schedule_name   => 'test_hourly_schedule',
    duration        => INTERVAL '60' MINUTE,
    window_priority => 'LOW',
    comments        => 'Window with a predefined schedule.');

  -- Window with an inline schedule.
  DBMS_SCHEDULER.create_window (
    window_name     => 'test_window_2',
    resource_plan   => NULL,
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=hourly; byminute=0',
    end_date        => NULL,
    duration        => INTERVAL '60' MINUTE,
    window_priority => 'LOW',
    comments        => 'Window with an inline schedule.');
END;
/

PL/SQL procedure successfully completed.

-- Display window group details.
SELECT window_name, resource_plan, enabled, active
FROM   dba_scheduler_windows;

WINDOW_NAME                    RESOURCE_PLAN                  ENABL ACTIV
------------------------------ ------------------------------ ----- -----
WEEKNIGHT_WINDOW                                              TRUE  FALSE
WEEKEND_WINDOW                                                TRUE  FALSE
TEST_WINDOW_1                                                 TRUE  FALSE
TEST_WINDOW_2                                                 TRUE  FALSE

4 rows selected.

Windows can be opened and closed manually using the OPEN_WINDOW and CLOSE_WINDOW procedures.

BEGIN
  -- Open window.
  DBMS_SCHEDULER.open_window (
   window_name => 'test_window_2',
   duration    => INTERVAL '1' MINUTE,
   force       => TRUE);
END;
/

PL/SQL procedure successfully completed.

-- Display window group details.
SELECT window_name, resource_plan, enabled, active
FROM   dba_scheduler_windows;

WINDOW_NAME                    RESOURCE_PLAN                  ENABL ACTIV
------------------------------ ------------------------------ ----- -----
WEEKNIGHT_WINDOW                                              TRUE  FALSE
WEEKEND_WINDOW                                                TRUE  FALSE
TEST_WINDOW_1                                                 TRUE  FALSE
TEST_WINDOW_2                                                 TRUE  TRUE

4 rows selected.

BEGIN
  -- Close window.
  DBMS_SCHEDULER.close_window (
   window_name => 'test_window_2');
END;
/

PL/SQL procedure successfully completed.

-- Display window group details.
SELECT window_name, resource_plan, enabled, active
FROM   dba_scheduler_windows;

WINDOW_NAME                    RESOURCE_PLAN                  ENABL ACTIV
------------------------------ ------------------------------ ----- -----
WEEKNIGHT_WINDOW                                              TRUE  FALSE
WEEKEND_WINDOW                                                TRUE  FALSE
TEST_WINDOW_1                                                 TRUE  FALSE
TEST_WINDOW_2                                                 TRUE  FALSE

4 rows selected.

Windows can be dropped using the DROP_WINDOW procedure.

BEGIN
  DBMS_SCHEDULER.drop_window (
    window_name => 'test_window_1',
    force       => TRUE);

  DBMS_SCHEDULER.drop_window (
    window_name => 'test_window_2',
    force       => TRUE);
END;
/

PL/SQL procedure successfully completed.

-- Display window group details.
SELECT window_name, resource_plan, enabled, active
FROM   dba_scheduler_windows;

WINDOW_NAME                    RESOURCE_PLAN                  ENABL ACTIV
------------------------------ ------------------------------ ----- -----
WEEKNIGHT_WINDOW                                              TRUE  FALSE
WEEKEND_WINDOW                                                TRUE  FALSE

2 rows selected.

Windows Groups

A window group is a collection of related windows. It can be created with 0, 1 or many windows as group members using the CREATE_WINDOW_GROUP procedure.

BEGIN
  DBMS_SCHEDULER.create_window_group (
    group_name  => 'test_window_group',
    window_list => 'test_window_1, test_window_2',
    comments    => 'A test window group');
END;
/

PL/SQL procedure successfully completed.

-- Display window group details.
SELECT window_group_name, enabled, number_of_windowS
FROM   dba_scheduler_window_groups;

WINDOW_GROUP_NAME              ENABL NUMBER_OF_WINDOWS
------------------------------ ----- -----------------
MAINTENANCE_WINDOW_GROUP       TRUE                  2
TEST_WINDOW_GROUP              TRUE                  2

2 rows selected.

Windows can be added and removed from a group using the ADD_WINDOW_GROUP_MEMBER and REMOVE_WINDOW_GROUP_MEMBER procedures.

BEGIN
  -- Create a new window.
  DBMS_SCHEDULER.create_window (
    window_name     => 'test_window_3',
    resource_plan   => NULL,
    schedule_name   => 'test_hourly_schedule',
    duration        => INTERVAL '60' MINUTE,
    window_priority => 'LOW',
    comments        => 'Window with a predefined schedule.');

  DBMS_SCHEDULER.add_window_group_member (
    group_name  => 'test_window_group',
    window_list => 'test_window_3');
END;
/

PL/SQL procedure successfully completed.

-- Display window group members.
SELECT window_group_name, window_name
FROM   dba_scheduler_wingroup_members;

WINDOW_GROUP_NAME              WINDOW_NAME
------------------------------ ------------------------------
MAINTENANCE_WINDOW_GROUP       WEEKNIGHT_WINDOW
MAINTENANCE_WINDOW_GROUP       WEEKEND_WINDOW
TEST_WINDOW_GROUP              TEST_WINDOW_1
TEST_WINDOW_GROUP              TEST_WINDOW_2
TEST_WINDOW_GROUP              TEST_WINDOW_3

5 rows selected.

BEGIN
  DBMS_SCHEDULER.remove_window_group_member (
    group_name  => 'test_window_group',
    window_list => 'test_window_3');
END;
/

PL/SQL procedure successfully completed.

-- Display window group members.
SELECT window_group_name, window_name
FROM   dba_scheduler_wingroup_members;

WINDOW_GROUP_NAME              WINDOW_NAME
------------------------------ ------------------------------
MAINTENANCE_WINDOW_GROUP       WEEKNIGHT_WINDOW
MAINTENANCE_WINDOW_GROUP       WEEKEND_WINDOW
TEST_WINDOW_GROUP              TEST_WINDOW_1
TEST_WINDOW_GROUP              TEST_WINDOW_2

4 rows selected.

Window groups can be dropped using the DROP_WINDOW_GROUP procedure.

BEGIN
  DBMS_SCHEDULER.drop_window_group (
    group_name => 'test_window_group',
    force      => TRUE);
END;
/

PL/SQL procedure successfully completed.

-- Display window group details.
SELECT window_group_name, enabled, number_of_windowS
FROM   dba_scheduler_window_groups;

WINDOW_GROUP_NAME              ENABL NUMBER_OF_WINDOWS
------------------------------ ----- -----------------
MAINTENANCE_WINDOW_GROUP       TRUE                  2

1 row selected.

The force option must be used if the window group currently has members.

Enable, Disable and Attributes

All applicable scheduler objects can be enabled and disabled using the overloaded ENABLE and DISABLE procedures.

BEGIN
  -- Enable programs and jobs.
  DBMS_SCHEDULER.enable (name => 'test_stored_procedure_prog');
  DBMS_SCHEDULER.enable (name => 'test_full_job_definition');

  -- Disable programs and jobs.
  DBMS_SCHEDULER.disable (name => 'test_stored_procedure_prog');
  DBMS_SCHEDULER.disable (name => 'test_full_job_definition');
END;
/

The values for individual attributes of all scheduler objects can be altered using one of the SET_ATTRIBUTE overloads.

BEGIN
  DBMS_SCHEDULER.set_attribute (
    name      => 'hourly_schedule',
    attribute => 'repeat_interval',
    value     => 'freq=hourly; byminute=30');
END;
/

The values can be set to NULL using the SET_ATTRIBUTE_NULL procedure.

BEGIN
  DBMS_SCHEDULER.set_attribute_null (
    name      => 'hourly_schedule',
    attribute => 'repeat_interval');
END;
/

Configuring The Scheduler

The SCHEDULER_ADMIN role gives a user the ability to control every aspect of the scheduler, as well as generating jobs to run as any other user. For this reason you should avoid granting it to anyone other than trusted DBAs.

For the majority of users, the CREATE JOB privilege will be sufficient.

For users requiring some level of scheduler administrative privileges, the MANAGE SCHEDULER privilege allows them to create additional scheduler objects, as well as allowing them to set and retrieve scheduler attributes using the SET_SCHEDULER_ATTRIBUTE and GET_SCHEDULER_ATTRIBUTE procedures.

Calendar Syntax Examples

The basic calendaring syntax is shown below.

repeat_interval = frequency_clause
  [; interval=?] [; bymonth=?] [; byweekno=?] 
  [; byyearday=?] [; bymonthday=?] [; byday=?] 
  [; byhour=?] [; byminute=?] [; bysecond=?]

frequency_clause = "FREQ" "=" frequency
   frequency = "YEARLY" | "MONTHLY" | "WEEKLY" | "DAILY" | 
   "HOURLY" | "MINUTELY" | "SECONDLY"

For a full syntax breakdown, see this and this.

The easiest way to get to grips with the calendaring syntax is by example, so this section presents several examples of how the syntax is used to schedule jobs at different intervals. The date and timestamp intervals are also listed for the sake of comparison.

The test_calendar_string.sql script is quite useful for testing calendaring syntax strings.

Every day

Repeat interval using calendaring syntax.

'freq=daily;'

Repeat interval using dates and timestamps.

'sysdate + 1'
'systimestamp + 1'
      
'sysdate + interval ''1'' day'
'systimestamp + interval ''1'' day'

Every day, at midnight

Repeat interval using calendaring syntax.

'freq=daily; byhour=0; byminute=0; bysecond=0;'

Repeat interval using dates and timestamps.

'trunc(sysdate) + 1'
'trunc(systimestamp) + 1'

'trunc(sysdate) + interval ''1'' day'
'trunc(systimestamp) + interval ''1'' day'

Every day, at 06:00

Repeat interval using calendaring syntax.

'freq=daily; byhour=6; byminute=0; bysecond=0;'

Repeat interval using dates and timestamps.

'trunc(sysdate) + 1 + 6/24'
'trunc(systimestamp) + 1 + 6/24'
      
'trunc(sysdate) + interval ''1 6'' day to hour '
'trunc(systimestamp) + interval ''1 6'' day to hour'

Every hour

Repeat interval using calendaring syntax.

'freq=hourly;'

Repeat interval using dates and timestamps.

'sysdate + 1/24'
'systimestamp + 1/24'
      
'sysdate + interval ''1'' hour'
'systimestamp + interval ''1'' hour'

Every hour, on the hour

Repeat interval using calendaring syntax.

'freq=hourly; byminute=0; bysecond=0;'

Repeat interval using dates and timestamps.

'trunc(sysdate, ''HH24'') + 1/24'
'trunc(systimestamp, ''HH24'') + 1/24'
      
'trunc(sysdate, ''HH24'') + interval ''1'' hour'
'trunc(systimestamp, ''HH24'') + interval ''1'' hour'

Every minute

Repeat interval using calendaring syntax.

'freq=minutely;'

Repeat interval using dates and timestamps.

'sysdate + 1/24/60'
'systimestamp + 1/24/60'
      
'sysdate + interval ''1'' minute'
'systimestamp + interval ''1'' minute'

Every minute, on the minute

Repeat interval using calendaring syntax.

'freq=minutely; bysecond=0;'

Repeat interval using dates and timestamps.

'trunc(sysdate, ''MI'') + 1/24/60'
'trunc(systimestamp, ''MI'') + 1/24/60'
      
'trunc(sysdate, ''MI'') + interval ''1'' minute'
'trunc(systimestamp, ''MI'') + interval ''1'' minute'

Every 5 minutes

Repeat interval using calendaring syntax.

'freq=minutely; interval=5; bysecond=0;'

Repeat interval using dates and timestamps.

'trunc(sysdate, ''MI'') + 5/24/60'
'trunc(systimestamp, ''MI'') + 5/24/60'
      
'trunc(sysdate, ''MI'') + interval ''5'' minute'
'trunc(systimestamp, ''MI'') + interval ''5'' minute'

Every Monday at 09:00

Repeat interval using calendaring syntax.

'freq=weekly; byday=mon; byhour=9; byminute=0; bysecond=0;'

Repeat interval using dates and timestamps.

'trunc(next_day(sysdate, ''MONDAY'')) + 9/24'
'trunc(next_day(systimestamp, ''MONDAY'')) + 9/24'
      
'trunc(next_day(sysdate, ''MONDAY'')) + interval ''9'' hour'
'trunc(next_day(systimestamp, ''MONDAY'')) + interval ''9''hour'

Every Monday, Wednesday and Friday at 06:00

Repeat interval using calendaring syntax.

'freq=weekly; byday=mon,wed,fri; byhour=6; byminute=0; bysecond=0;'

Repeat interval using dates and timestamps.

'trunc(least(next_day(sysdate, ''monday''), next_day(sysdate, ''wednesday''), next_day(sysdate, ''friday''))) + (6/24)'
      
'trunc(least(next_day(systimestamp, ''monday''), next_day(systimestamp, ''wednesday''), next_day(systimestamp, ''friday''))) + (6/24)'
      
'trunc(least(next_day(sysdate,''monday''), next_day(sysdate, ''wednesday''), next_day(sysdate, ''friday''))) + interval ''6'' hour'

'trunc(least(next_day(systimestamp, ''monday''), next_day(systimestamp, ''wednesday''), next_day(systimestamp, ''friday''))) + interval ''6'' hour'

First Monday of each quarter

Repeat interval using calendaring syntax.

'freq=monthly; bymonth=1,4,7,10; byday=1mon'

Repeat interval using dates and timestamps.

'next_day(add_months(trunc(sysdate, ''q''), 3), ''monday'')'
'next_day(add_months(trunc(systimestamp, ''q''), 3), ''monday'')'

Extracting DDL

The script used to create scheduler objects can be extracted using the DBMS_METADATA package, as shown in the following example.

CONN test/test

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name   => 'MY_TEST_JOB',
    job_type   => 'PLSQL_BLOCK',
    job_action => 'BEGIN NULL; END;',
    start_date => TRUNC(SYSDATE), 
    repeat_interval => 'FREQ=monthly;BYDAY=SUN;BYHOUR=22;BYMINUTE=0;BYSECOND=0');
END;
/

SET LONG 100000
SELECT DBMS_METADATA.get_ddl('PROCOBJ','MY_TEST_JOB', 'TEST') AS job_def FROM dual;
JOB_DEF
--------------------------------------------------------------------------------


BEGIN
dbms_scheduler.create_job('"MY_TEST_JOB"',
job_type=>'PLSQL_BLOCK', job_action=>
'BEGIN NULL; END;'
, number_of_arguments=>0,
start_date=>TO_TIMESTAMP_TZ('04-APR-2012 12.00.00.000000000 AM +01:00','DD-MON-R
RRR HH.MI.SSXFF AM TZR','NLS_DATE_LANGUAGE=english'), repeat_interval=>
'FREQ=monthly;BYDAY=SUN;BYHOUR=22;BYMINUTE=0;BYSECOND=0'
, end_date=>NULL,

JOB_DEF
--------------------------------------------------------------------------------
job_class=>'"DEFAULT_JOB_CLASS"', enabled=>FALSE, auto_drop=>TRUE,comments=>
NULL
);
COMMIT;
END;


SQL>

Clean up the test job using the floowing.

EXEC DBMS_SCHEDULER.drop_job('MY_TEST_JOB');

Scheduler Views

A number of DBA_SCHEDULER_%, ALL_SCHEDULER_% and USER_SCHEDULER_% views are available to display information about scheduler objects. In Oracle 10g, the following views are present.

The DBA_SCHEDULER_JOB_RUN_DETAILS view is especially interesting as it provides a history of the job runs, including the status of the run and error messages associated with failed runs.

Later database releases include additional views to support new functionality.

Time Zones

The way the scheduler handles time zones can be a little confusing at first. If the scheduler default_timezone is not specified, it attempts to determine it from the OS. If that isn't possible it is set to NULL. You can check the current default_timezone using the following query.

SELECT DBMS_SCHEDULER.STIME FROM DUAL;

STIME
---------------------------------------------------------------------------
04-APR-2012 16.11.59.441143000 EUROPE/LONDON

1 row selected.

SQL>

Rather than leaving the default_timezone setting to chance, it can be explicitly set using the following command.

BEGIN
  DBMS_SCHEDULER.set_scheduler_attribute(
    attribute => 'default_timezone',
    value     => 'EUROPE/LONDON');
END;
/

When you create a job, the time zone for the job is determined using time zone associated with the start_date parameter. If that is not supplied, the default_timezone is used. In the case of the start_date value, using SYSTIMESTAMP will fix an offset, rather than honouring the daylight savings rules. A more consistent result is produced from using a method such as this, where the time zone is specified explicitly.

start_date => TO_TIMESTAMP_TZ(TO_CHAR(SYSTIMESTAMP, 'DDMMYYYY HH24:MI:SS')||' EUROPE/LONDON', 'DDMMYYYY HH24:MI:SS TZR')

You can switch an existing job from using an offset to an explicit timezone by resetting its start date attribute.

BEGIN
  DBMS_SCHEDULER.set_attribute (
    name      => 'MY_SCHEMA.MY_JOB',
    attribute => 'start_date',
    value     => TO_TIMESTAMP_TZ(TO_CHAR(SYSTIMESTAMP, 'DDMMYYYY HH24:MI:SS')||' EUROPE/LONDON', 'DDMMYYYY HH24:MI:SS TZR'));
END;
/

Implicit Commit

As a cautionary note, calls to the DBMS_SCHEDULER package come with an implicit commit, so you can't make your job creation transactional, like you could with DBMS_JOB. If you need to include the creation of a job as part of a larger transaction, you will still need to use the old scheduler and DBMS_JOB, until Oracle provide the option of controlling the commit processing in the new scheduler. An example of the difference is shown below.

Using the DBMS_JOB package, job creation is transactional and under your control.

-- Create job, but don't explicitly commit.
SET SERVEROUTPUT ON
DECLARE
  l_job BINARY_INTEGER;
BEGIN
  DBMS_JOB.submit(
    job       => l_job,
    what      => 'BEGIN NULL; END;',
    next_date => TRUNC(SYSDATE, 'HH24') + (1/24),
    interval  => 'TRUNC(SYSDATE, ''HH24'') + (1/24)');
    
  DBMS_OUTPUT.put_line('Job=' || l_job);
END;
/
Job=1

PL/SQL procedure successfully completed.

SQL>

-- Check it is there.
COLUMN what FORMAT A50

SELECT job,
       next_date,
       what
FROM   user_jobs
WHERE  job = 1;

       JOB NEXT_DATE            WHAT
---------- -------------------- --------------------------------------------------
         1 05-JUN-2015 13:00:00 BEGIN NULL; END;

1 row selected.

SQL>

-- Rollback and check it has gone.
ROLLBACK;

SELECT job,
       next_date,
       what
FROM   user_jobs
WHERE  job = 1;

no rows selected

SQL>

Using the DBMS_SCHEDULER package, job creation is not transactional, as job creation includes an implicit commit.

-- Create job, but don't explicitly commit.
BEGIN
  DBMS_SCHEDULER.create_job (
    job_name        => 'TEST_FULL_JOB_DEFINITION',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN NULL; END;',
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=hourly; byminute=0; bysecond=0;',
    enabled         => TRUE);
END;
/

-- Rollback and check if job is still there.
ROLLBACK;

SELECT job_name
FROM   user_scheduler_jobs
WHERE  job_name = 'TEST_FULL_JOB_DEFINITION';

JOB_NAME
------------------------------
TEST_FULL_JOB_DEFINITION

1 row selected.

SQL>

Scheduler Features List

Here are quick links to articles on scheduler features that span multiple versions.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.