Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> DBMS_JOB scheduling

DBMS_JOB scheduling

From: Garry Gillies <g.gillies_at_weir.co.uk>
Date: Wed, 23 Jul 2003 10:03:28 +0100
Message-Id: <25988.339009@fatcity.com>


Any Interest?

The DBMS_JOB package is supplied by Oracle to allow the running of procedures at regular
intervals. Unfortunately the INTERVAL parameter is limited to 128 characters, which prevents
you from getting very complex (user defined functions [in the interval parameter] do not
work well - according to Fuerstein in his book Oracle Built In Packages).

The situation is eased somewhat by the fact that the NEXT_DATE parameter can be supplied
to the procedure as an in/out parameter - and the procedure can contain whatever code is
necessary to calculate when next to run. This is all very well, but custom coding scheduling routines can quickly become tedious.
On the basis of "do it once and get it over with" I have written a function called NEXT_DATE
which I have wrapped in a package called CRON.

There is a Unix program called cron which runs jobs on a regular basis. Although the scheduling
data supplied to cron is simple and concise, complex schedules are easy to specify.

The NEXT_DATE function takes in a cron schedule string and returns the next date that
conforms to the schedule - or you can supply a cron schedule and a date and it will return the
first date after the supplied date that conforms to the schedule. At the moment it is not very friendly on the error detection front. A VALUE_ERROR is
returned if it deems the cron schedule to be invalid. You will also get a VALUE_ERROR
if the next valid date is more than twenty seven years in the future. DBMS_OUTPUT is used to display error messages which will hopefully give you a clue.
This will be improved if I receive enough complaints ( and suggestions for improvements).

THE CRON SCHEDULE A cron schedule consists of five components, each separated from the next by a space.
The syntax is identical for all components. The components represent

         Minute in Hour
         Hour in day
         Day in month
         Month in year
         Day of Week - A bit of a bugger this one. In Unix land the day 
numbering runs
         from 0-6 with 0 being Sunday. In Oracle the day numbering depends 
on the
         setting of NLS_TERRITORY.
         I have chosen to go with ISO standard  8601:1998 which runs from 
1-7
         with 1 being Monday. This is so close to the Unix convention that 
I can interpret
         Unix cron schedules correctly.
         Curiously, Oracle do not provide a date format which supplies 
this number.
         The ISO week number is available with the format 'IW', but not 
the ISO day
         number. If you have a field of type date called dt, you can 
obtain the ISO day 
         number with
         ( trunc(dt) - trunc(dt ,'IW') ) + 1
A component can consist of

   an asterisk * which represents all valid values

        or
   a number of elements separated by a comma (if only one element is supplied,

   forget the comma). An element can be    a single number - valid for the component (32 in "Day in month" is invalid)

         or
   two numbers separated by a hyphen - which represents a range.

EXAMPLES Run every hour on the hour

     0 * * * *
Run twice every hour, on the hour and on the half hour

     0,30 * * * *
Run twice every hour, on the hour and on the half hour between 08:00 and 16:59

     0,30 8-16 * * *
Run twice every hour, on the hour and on the half hour between 08:00 and 16:59, Monday to Friday

    0,30 8-16 * * 1-5
Run at 11:12 every Friday the 13th

    11 12 13 * 5
Run at 04:00 every leap year on february 29

    0 4 29 2 *
Run at 04:00 every leap year on february 29 when february 29 is a Thursday

    0 4 29 2 4

Garry Gillies
Database Administrator
Business Systems
Weir Pumps Ltd
149 Newlands Road, Cathcart, Glasgow, G44 4EX

T: +44 0141 308 3982
F: +44 0141 633 1147
E: g.gillies_at_weirpumps.com

CONFIDENTIAL: The information contained in this email (including any attachments) is confidential, subject to copyright and for the use of the intended recipient only. If you are not the intended recipient please delete this message after notifying the sender. Unauthorised retention, alteration or distribution of this email is forbidden and may be actionable.

Attachments are opened at your own risk and you are advised to scan incoming email for viruses before opening any attached files. We give no guarantee that any communication is virus-free and accept Received on Wed Jul 23 2003 - 04:03:28 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US