Scheduling Jobs

The Oracle package dbms_job can be used to schedule jobs. The procedure dbms_job.submit() accepts three parameters:
  • the name of the job to submit
  • the start time for the job
  • the interval to execute the job
Example
DBMS_JOB.SUBMIT (
 job => :jobno
 , what => 'statspack.snap;' -- What to run
 , next_date => TRUNC(sysdate+1/24,'HH') -- Start next hour
 , interval => 'TRUNC(SYSDATE+1/24,''HH'')' -- Run every hour
); 


SQL> VARIABLE  jobno   NUMBER;
Submit job to begin at 0600 and run every hour
SQL> EXEC DBMS_JOB.SUBMIT (:jobno, 'statspack.snap;' , TRUNC(sysdate+1)+6/24, 'TRUNC(SYSDATE+1/24,''HH'')' );
Submit job to begin at 0900 and run 12 hours later
SQL> EXEC  DBMS_JOB.SUBMIT (:jobno, 'statspack.snap;' , TRUNC(sysdate+1)+9/24 , 'TRUNC(SYSDATE+12/24,''HH'')'  );
Submit job to begin at 0600 and run every 10 minutes
SQL> EXEC DBMS_JOB.SUBMIT (:jobno , 'statspack.snap;' , TRUNC(sysdate+1)+6/24, 'TRUNC(sysdate+10/1440,''MI'')'  );
Submit job to begin at 0600 and run every hour, Monday - Friday
SQL > EXEC DBMS_JOB.SUBMIT (:jobno, 'statspack.snap;', TRUNC(sysdate+1)+6/24 , 'decode(to_char(sysdate+1,''Dy''), 'SAT', sysdate+3, ''SUN'', sysdate+2,sysdate+1)'  );
Job execution intervals are determined by the date expression set by the interval parameter. Two different types of jobs can be scheduled
  1. Each execution of the job should follow the last by a specific time interval.
  2. The job should execute on specific dates and times.
Job Intervals and Date Arithmetic

Type 1 Jobs

Jobs of type 1 usually have relatively simple date arithmetic expressions
of the type SYSDATE+N, where N represents the time interval
expressed in days. The following table provides examples of these types of intervals.
The job intervals only expresses that the spacing between executions will be at least that specified.
For instance, if a job is first executed at 12:00 p.m. with in
interval of 'SYSTEM + 1', it will be scheduled to execute the next day at 12:00 p.m.
However, if a user executes the job manually at 4:00 p.m. the next
day using DBMS_JOB.RUN, then it will be rescheduled for execution at 4:00 p.m. the next day.

If the database is down or the job queue so busy that the job cannot be executed exactly at the time scheduled then the job
will run as soon as it can, but the execution time will be different.
For Type I jobs with jobs with simple interval expressions this "drift" in next execution times is characteristic.

Action Interval Time
Execute daily 'SYSDATE + 1'
Execute every 4 hours 'SYSDATE + 4/24'
Execute every 10 minutes 'SYSDATE + 10/1440'
Execute every 30 seconds 'SYSDATE + 30/86400'
Execute every 7 days 'SYSDATE + 7'
Do no re-execute and remove job NULL

Type 2 Jobs

Jobs with type 2 execution requirements involve more complex interval date expressions, as see in the following table.

Action Interval Time
Every day at 12:00 midnight TRUNC(SYSDATE + 1)
Every day at 8:00 p.m. TRUNC(SYSDATE + 1) + 20/24
Every Tuesday at 12:00 noon NEXT_DAY(TRUNC(SYSDATE), "TUESDAY") + 12/24
First day of the month at midnight TRUNC(LAST_DAY(SYSDATE) + 1)
Last day of the quarter at 11:00 p.m. TRUNC(ADD_MONTH(SYSDATE + 2/24,3),'Q') - 1/24
Every Monday, Wednesday and Friday at 9:00 p.m.
decode(to_char(sysdate+1, 'DY'), 
'MON', trunc(sysdate+1)+21/24,
'WED', trunc(sysdate+1)+21/24,
'FRI', trunc(sysdate+1)+21/24,
'SUN', trunc(sysdate+2)+21/24,
'TUE', trunc(sysdate+2)+21/24,
'THU', trunc(sysdate+2)+21/24,
'SAT', trunc(sysdate+3)+21/24)



Oracle Date Calculations Examples
Date / Time Fraction Description
WHERE (date column) > sysdate - (6/24); Past 6 hours. (or 1/4 of a day ago)
WHERE (date column) > sysdate - 6; Past six days
WHERE (date column) > sysdate - (6/1440); Past six minutes
1/(24*3600) One second
1/1440 One minute
1/24 One hour
TRUNC(SYSDATE+1/24,'HH') Every one hour starting with the next hour
TRUNC(SYSDATE+1/144,'Mi') Every 10 Minutes starting with next minute

My Oracle Debug Guide