Oracle DBMS_SCHEDULER.CREATE_JOB Example: Daily, Weekly, One-Time

Oracle DBMS_SCHEDULER.CREATE_JOB Example: Daily, Weekly, One-Time
In this article
  1. How DBMS_SCHEDULER works
  2. Sample setup
  3. Example 1: a daily job with a PL/SQL block
  4. Example 2: run a stored procedure with an argument
  5. Example 3: a one-time job
  6. repeat_interval examples you can copy
  7. Check job status and run history
  8. Run, change, stop and drop a job
  9. Common mistakes
  10. Best practices
  11. Frequently asked questions
  12. Summary

To schedule a job in Oracle, call DBMS_SCHEDULER.CREATE_JOB with a job name, a job type, the code to run and a repeat_interval such as FREQ=DAILY;BYHOUR=1. Set enabled => TRUE, because jobs are created disabled by default and a disabled job never runs. Forgetting it is a very common reason a new job "does nothing".

This tutorial walks through complete DBMS_SCHEDULER.CREATE_JOB examples on a small orders table: a daily PL/SQL block, a weekly stored procedure with an argument, a one-time job, and the calendar strings people search for most (every 15 minutes, weekdays only, last day of the month). You will also see how to test a schedule before you trust it, how to read the job history, and how to fix the mistakes that make jobs fail quietly.

How DBMS_SCHEDULER works

Oracle Scheduler is built into the database. You create a job, which pairs what to run (the job action) with when to run it (the schedule). A background coordinator process wakes up when a job is due and hands it to a job worker process, so the job runs inside the database even when nobody is logged in.

The number of job worker processes is limited by the JOB_QUEUE_PROCESSES initialization parameter. If it is set to 0, Scheduler jobs and old DBMS_JOB jobs do not run at all. Oracle recommends DBMS_SCHEDULER over DBMS_JOB, which is kept only for backward compatibility.

The simplest form of CREATE_JOB takes everything inline. This is the signature from the Oracle 19c documentation:

DBMS_SCHEDULER.CREATE_JOB (
   job_name             IN VARCHAR2,
   job_type             IN VARCHAR2,
   job_action           IN VARCHAR2,
   number_of_arguments  IN PLS_INTEGER              DEFAULT 0,
   start_date           IN TIMESTAMP WITH TIME ZONE DEFAULT NULL,
   repeat_interval      IN VARCHAR2                 DEFAULT NULL,
   end_date             IN TIMESTAMP WITH TIME ZONE DEFAULT NULL,
   job_class            IN VARCHAR2                 DEFAULT 'DEFAULT_JOB_CLASS',
   enabled              IN BOOLEAN                  DEFAULT FALSE,
   auto_drop            IN BOOLEAN                  DEFAULT TRUE,
   comments             IN VARCHAR2                 DEFAULT NULL,
   credential_name      IN VARCHAR2                 DEFAULT NULL,
   destination_name     IN VARCHAR2                 DEFAULT NULL);
CREATE_JOB signature (inline program and schedule)

These are the parameters you will use almost every time:

ParameterWhat it meansDefault
job_nameName of the job in your schemaRequired
job_typePLSQL_BLOCK, STORED_PROCEDURE, EXECUTABLE, CHAIN and othersRequired
job_actionThe PL/SQL block, procedure name or program path to runRequired
start_dateFirst run time; also the reference date and time zone for the calendarNULL
repeat_intervalCalendar string such as FREQ=DAILY. NULL means run onceNULL
enabledTRUE schedules the job immediatelyFALSE
auto_dropTRUE drops the job once it has completedTRUE

If both start_date and repeat_interval are NULL, the job runs as soon as it is enabled. If you give a start_date but no repeat_interval, it runs once at that time.

Sample setup

Create two small tables and two procedures. The jobs in the following sections run these procedures, so you can follow along in any test schema that has the CREATE JOB privilege.

CREATE TABLE orders (
  order_id      NUMBER PRIMARY KEY,
  customer_name VARCHAR2(50),
  amount        NUMBER(10,2),
  status        VARCHAR2(20),
  order_date    DATE
);

CREATE TABLE order_daily_summary (
  summary_date  DATE PRIMARY KEY,
  order_count   NUMBER,
  total_amount  NUMBER(12,2),
  created_at    TIMESTAMP DEFAULT SYSTIMESTAMP
);

INSERT INTO orders VALUES (1, 'Asha',  1200, 'PAID',      TRUNC(SYSDATE) - 1 + 10/24);
INSERT INTO orders VALUES (2, 'Ravi',   850, 'PAID',      TRUNC(SYSDATE) - 1 + 15/24);
INSERT INTO orders VALUES (3, 'Meera',  400, 'CANCELLED', TRUNC(SYSDATE) - 120);
INSERT INTO orders VALUES (4, 'John',   990, 'CANCELLED', TRUNC(SYSDATE) - 5);
COMMIT;
Sample tables and data
CREATE OR REPLACE PROCEDURE build_daily_summary AS
  l_day DATE := TRUNC(SYSDATE) - 1;
BEGIN
  DELETE FROM order_daily_summary WHERE summary_date = l_day;

  INSERT INTO order_daily_summary (summary_date, order_count, total_amount)
  SELECT l_day, COUNT(*), NVL(SUM(amount), 0)
    FROM orders
   WHERE order_date >= l_day
     AND order_date <  l_day + 1;

  COMMIT;
END;
/

CREATE OR REPLACE PROCEDURE purge_cancelled_orders (p_days IN NUMBER) AS
BEGIN
  DELETE FROM orders
   WHERE status = 'CANCELLED'
     AND order_date < SYSDATE - p_days;
  COMMIT;
END;
/
Procedures the jobs will call

The summary procedure deletes and re-inserts yesterday's row, so running it twice gives the same result. Make scheduled code safe to rerun like this, because you will run it by hand while testing and the Scheduler may retry it.

Example 1: a daily job with a PL/SQL block

This job runs build_daily_summary every day at 1:00 AM India time.

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name        => 'DAILY_ORDER_SUMMARY',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN build_daily_summary; END;',
    start_date      => TO_TIMESTAMP_TZ('2026-11-11 01:00:00 Asia/Kolkata',
                                       'YYYY-MM-DD HH24:MI:SS TZR'),
    repeat_interval => 'FREQ=DAILY;BYHOUR=1;BYMINUTE=0;BYSECOND=0',
    enabled         => TRUE,
    comments        => 'Builds yesterday''s order summary');
END;
/
Create a daily job
PL/SQL procedure successfully completed.
Output (expected output)

A few things are happening here. The job_action for a PLSQL_BLOCK job is a complete anonymous block, and it must end with a semicolon. The calendar string sets the hour, minute and second explicitly. Without BYMINUTE and BYSECOND, those values are taken from the start date.

The start_date uses a region name (Asia/Kolkata) rather than an offset like +05:30. The calendar syntax has no time zone of its own; the Scheduler takes it from the start date. With a region name, jobs in regions that observe daylight saving time follow the clock changes. With a fixed offset they do not, and the job runs an hour off for half the year.

Example 2: run a stored procedure with an argument

When the job action is a procedure, use job_type => STORED_PROCEDURE and pass only the procedure name. Arguments are not written into the action. You declare how many there are with number_of_arguments and then set each one with SET_JOB_ARGUMENT_VALUE.

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name            => 'PURGE_CANCELLED_JOB',
    job_type            => 'STORED_PROCEDURE',
    job_action          => 'PURGE_CANCELLED_ORDERS',
    number_of_arguments => 1,
    start_date          => SYSTIMESTAMP,
    repeat_interval     => 'FREQ=WEEKLY;BYDAY=SUN;BYHOUR=2;BYMINUTE=0;BYSECOND=0',
    enabled             => FALSE);

  DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE (
    job_name          => 'PURGE_CANCELLED_JOB',
    argument_position => 1,
    argument_value    => '90');

  DBMS_SCHEDULER.ENABLE('PURGE_CANCELLED_JOB');
END;
/
Weekly job that calls a procedure with one argument

The job is created with enabled => FALSE on purpose. Oracle's documentation says to define all arguments before you enable a job, so the order is: create disabled, set arguments, then enable. The value is passed as a string ('90'); for non-VARCHAR2 values you can use SET_JOB_ANYDATA_VALUE.

This job uses SYSTIMESTAMP as the start date, which is convenient but takes the time zone of the database server. That is fine for a weekly cleanup. For jobs that must fire at a precise local time, use an explicit region name as in Example 1.

Example 3: a one-time job

Leave repeat_interval out and the job runs once at its start date. Because auto_drop defaults to TRUE, the job is dropped after it completes, which is what you usually want for a one-off task.

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name   => 'ONE_TIME_ORDERS_STATS',
    job_type   => 'PLSQL_BLOCK',
    job_action => 'BEGIN DBMS_STATS.GATHER_TABLE_STATS(USER, ''ORDERS''); END;',
    start_date => SYSTIMESTAMP + INTERVAL '10' MINUTE,
    enabled    => TRUE);
END;
/
Run once, ten minutes from now

Use this pattern to push slow work into the background. For example, a screen can submit a long report job and return immediately instead of waiting. If you want to keep the job definition after it runs, for example to look at its settings later, set auto_drop => FALSE.

repeat_interval examples you can copy

The calendar string always starts with FREQ. Valid frequencies are YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY and SECONDLY. INTERVAL is a positive whole number from 1 to 99 (default 1), and the BY clauses narrow down the exact times. The strings are not case-sensitive.

Requirementrepeat_interval
Every 15 minutesFREQ=MINUTELY;INTERVAL=15
Every 2 hoursFREQ=HOURLY;INTERVAL=2
Every day at 11:30 PMFREQ=DAILY;BYHOUR=23;BYMINUTE=30;BYSECOND=0
Weekdays at 9:30 AMFREQ=WEEKLY;BYDAY=MON,TUE,WED,THU,FRI;BYHOUR=9;BYMINUTE=30;BYSECOND=0
Twice a day, 8 AM and 8 PMFREQ=DAILY;BYHOUR=8,20;BYMINUTE=0;BYSECOND=0
Last day of every monthFREQ=MONTHLY;BYMONTHDAY=-1;BYHOUR=22;BYMINUTE=0;BYSECOND=0
Last Friday of every monthFREQ=MONTHLY;BYDAY=-1FRI;BYHOUR=18;BYMINUTE=0;BYSECOND=0
Last working day of the monthFREQ=MONTHLY;BYDAY=MON,TUE,WED,THU,FRI;BYSETPOS=-1

Test a calendar string before you use it

You do not have to wait for a job to fire to find out whether its schedule is right. DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING returns the next run date after a given time. Call it in a loop, feeding each result back in, to list the next few runs.

SET SERVEROUTPUT ON
DECLARE
  l_start TIMESTAMP WITH TIME ZONE :=
    TO_TIMESTAMP_TZ('2026-11-10 10:00:00 Asia/Kolkata',
                    'YYYY-MM-DD HH24:MI:SS TZR');
  l_after TIMESTAMP WITH TIME ZONE := l_start;
  l_next  TIMESTAMP WITH TIME ZONE;
BEGIN
  FOR i IN 1 .. 5 LOOP
    DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING(
      calendar_string   => 'FREQ=WEEKLY;BYDAY=MON,TUE,WED,THU,FRI;BYHOUR=9;BYMINUTE=30;BYSECOND=0',
      start_date        => l_start,
      return_date_after => l_after,
      next_run_date     => l_next);
    DBMS_OUTPUT.PUT_LINE(TO_CHAR(l_next, 'DY DD-MON-YYYY HH24:MI TZR'));
    l_after := l_next;
  END LOOP;
END;
/
List the next five run times of a weekday schedule
WED 11-NOV-2026 09:30 ASIA/KOLKATA
THU 12-NOV-2026 09:30 ASIA/KOLKATA
FRI 13-NOV-2026 09:30 ASIA/KOLKATA
MON 16-NOV-2026 09:30 ASIA/KOLKATA
TUE 17-NOV-2026 09:30 ASIA/KOLKATA
Output (expected output)

The start time is Tuesday 10:00, which is already past 9:30, so the first run is Wednesday. Saturday and Sunday are skipped. If the list does not match what the business asked for, fix the string now instead of after a missed run.

Check job status and run history

Is the job scheduled?

USER_SCHEDULER_JOBS shows the jobs you own, whether they are enabled, their current state and when they will run next. Use DBA_SCHEDULER_JOBS to see every job in the database.

SELECT job_name,
       enabled,
       state,
       TO_CHAR(next_run_date, 'DD-MON-YYYY HH24:MI') AS next_run,
       run_count,
       failure_count
  FROM user_scheduler_jobs
 ORDER BY job_name;
Your jobs and their next run
JOB_NAMEENABLEDSTATENEXT_RUNRUN_COUNTFAILURE_COUNT
DAILY_ORDER_SUMMARYTRUESCHEDULED11-NOV-2026 01:0000
ONE_TIME_ORDERS_STATSTRUESCHEDULED10-NOV-2026 10:2500
PURGE_CANCELLED_JOBTRUESCHEDULED15-NOV-2026 02:0000

Output (expected output). SCHEDULED means the job is waiting for its next run. Other states you will see include RUNNING, DISABLED, COMPLETED, FAILED and BROKEN. A job is marked broken after it has failed more times than MAX_FAILURES allows. If a job you just created shows DISABLED, you forgot enabled => TRUE or the ENABLE call.

Did the last run succeed?

Each run is logged in USER_SCHEDULER_JOB_RUN_DETAILS (or DBA_SCHEDULER_JOB_RUN_DETAILS). The ERRORS column holds the error text when a run fails, which is the first place to look when a job "did not work".

SELECT job_name,
       status,
       TO_CHAR(actual_start_date, 'DD-MON HH24:MI') AS started,
       run_duration,
       error#,
       SUBSTR(errors, 1, 60) AS error_text
  FROM user_scheduler_job_run_details
 WHERE log_date > SYSTIMESTAMP - INTERVAL '7' DAY
 ORDER BY log_date DESC;
Run history for the last seven days
JOB_NAMESTATUSSTARTEDRUN_DURATIONERROR#ERROR_TEXT
DAILY_ORDER_SUMMARYSUCCEEDED12-NOV 01:00+000 00:00:010
DAILY_ORDER_SUMMARYFAILED11-NOV 01:00+000 00:00:00942ORA-00942: table or view does not exist

Output (expected output, illustrative rows). Here the first run failed because a table the procedure used was missing or not granted, and the next run succeeded after the fix. ERROR# gives the ORA number without the prefix. The log is kept for 30 days by default (the Scheduler log_history attribute), so older runs disappear on their own.

Run, change, stop and drop a job

These calls cover day-to-day job maintenance:

-- Run now, in your own session, to test it
EXEC DBMS_SCHEDULER.RUN_JOB('DAILY_ORDER_SUMMARY');

-- Run now in the background, like a normal scheduled run
EXEC DBMS_SCHEDULER.RUN_JOB('DAILY_ORDER_SUMMARY', use_current_session => FALSE);

-- Change the schedule to 2:30 AM
BEGIN
  DBMS_SCHEDULER.SET_ATTRIBUTE(
    name      => 'DAILY_ORDER_SUMMARY',
    attribute => 'repeat_interval',
    value     => 'FREQ=DAILY;BYHOUR=2;BYMINUTE=30;BYSECOND=0');
END;
/

-- Pause and resume
EXEC DBMS_SCHEDULER.DISABLE('DAILY_ORDER_SUMMARY');
EXEC DBMS_SCHEDULER.ENABLE('DAILY_ORDER_SUMMARY');

-- Stop a running job, then drop it
EXEC DBMS_SCHEDULER.STOP_JOB('DAILY_ORDER_SUMMARY');
EXEC DBMS_SCHEDULER.DROP_JOB('DAILY_ORDER_SUMMARY');
Common job maintenance calls

RUN_JOB defaults to use_current_session => TRUE. The job runs in your session, the call waits until it finishes, and any error comes straight back to you. That is ideal for testing, but it does not update run_count, state or last_start_date in USER_SCHEDULER_JOBS. With FALSE, the job runs in a background worker and you check the run details view for errors.

SET_ATTRIBUTE on an enabled job disables it, applies the change and enables it again, so you do not need to disable it yourself. DROP_JOB fails if the job is running; pass force => TRUE to stop it first or defer => TRUE to let the current run finish, but not both.

Common mistakes

  • Forgetting enabled => TRUE. The default is FALSE. The job exists but the Scheduler ignores it. Check ENABLED in USER_SCHEDULER_JOBS.
  • Missing privilege. You need the CREATE JOB system privilege to create a job in your own schema, and CREATE ANY JOB for another schema. An EXECUTABLE job also needs CREATE EXTERNAL JOB.
  • Relying on roles inside the job. The job runs as its owner, and the procedures it calls follow normal definer's-rights rules: privileges that come only through a role are not available to stored code. Grant table access directly to the job owner. This is why a procedure that works in SQL Developer can fail as a job with ORA-00942.
  • Broken quotes or a missing semicolon in job_action. 'BEGIN build_daily_summary END;' fails at run time, not at create time. Test the block on its own first.
  • Putting arguments in a STORED_PROCEDURE action. job_action => 'PURGE_CANCELLED_ORDERS(90)' is wrong. Use number_of_arguments and SET_JOB_ARGUMENT_VALUE.
  • Using an offset instead of a region time zone. A start date with -05:00 ignores daylight saving changes. Use a region name such as America/New_York.
  • Online DDL in the job action. The job action runs inside an autonomous transaction, and Oracle's documentation notes that online DDL operations cannot be used there.
  • JOB_QUEUE_PROCESSES set to 0. If no job runs at all, ask your DBA to check this parameter. In a CDB, 0 in the root stops jobs in every PDB.

Best practices

  • Name jobs after what they do (DAILY_ORDER_SUMMARY, not JOB1) and fill in comments. Six months later someone will thank you.
  • Keep the logic in a procedure and keep job_action to a one-line call. The procedure can be compiled, tested and versioned like any other code.
  • Make the job code safe to rerun, as the summary procedure in this tutorial is.
  • Test the schedule with EVALUATE_CALENDAR_STRING and the code with RUN_JOB before the first real run.
  • Check USER_SCHEDULER_JOB_RUN_DETAILS for FAILED rows regularly, or build an alert on it. A job that fails every night without anyone noticing is worse than no job.
  • Avoid very frequent jobs (every few seconds) for work that could be triggered by an event or done in a single batch. Each run has startup overhead.

Frequently asked questions

How do I run a DBMS_SCHEDULER job every 5 minutes?

Use repeat_interval => 'FREQ=MINUTELY;INTERVAL=5'. The runs are spaced from the start date, so set start_date to a round time if you want runs at :00, :05, :10 and so on.

What is the difference between DBMS_JOB and DBMS_SCHEDULER?

DBMS_JOB is the older package, kept for backward compatibility. DBMS_SCHEDULER adds calendar strings, run history views, job classes, chains, external jobs and much more. Oracle recommends DBMS_SCHEDULER for all new jobs.

Why is my DBMS_SCHEDULER job not running?

Check, in order: ENABLED and STATE in USER_SCHEDULER_JOBS, NEXT_RUN_DATE (is it in the time zone you expect?), FAILED rows in USER_SCHEDULER_JOB_RUN_DETAILS, and finally the JOB_QUEUE_PROCESSES parameter.

How do I create a one-time job in Oracle?

Give a start_date and leave repeat_interval NULL. With the default auto_drop => TRUE, the job drops itself after it completes.

What privileges are needed for DBMS_SCHEDULER.CREATE_JOB?

CREATE JOB for your own schema, CREATE ANY JOB for other schemas, and CREATE EXTERNAL JOB for EXECUTABLE jobs. Force-stopping jobs needs MANAGE SCHEDULER.

Summary

  • Create jobs with DBMS_SCHEDULER.CREATE_JOB and set enabled => TRUE (the default is FALSE).
  • Use PLSQL_BLOCK for short blocks and STORED_PROCEDURE plus SET_JOB_ARGUMENT_VALUE for procedures with arguments.
  • Leave repeat_interval NULL for a one-time job; auto_drop removes it afterwards.
  • Give start_date a region time zone and test the calendar string with EVALUATE_CALENDAR_STRING.
  • Check USER_SCHEDULER_JOBS for state and USER_SCHEDULER_JOB_RUN_DETAILS for errors.
  • Grant table privileges directly to the job owner, not through a role.
PT Ganesh

An experienced PostgreSQL DBA/SQL developer with years of expertise in designing and implementing robust database solutions. Proficient in writing complex queries, developing stored procedures, and conducting performance tuning to deliver highly efficient data-driven applications that meet business requirements.

Home
SQL
PLSQL
Interview Q&A