Search code examples
sqloracle-databaseoracle-sqldeveloperdatabase-administration

Can we call DBMS_JOB.RUN in the background?


I insert some jobs to the job queue with the help of DBMS_JOB.SUBMIT.

Is there anyway that I run them in the background in the next step using DBMS_JOB.RUN?

I don't want the users wait for the DBMS_JOB.RUN.

THANKS!


Solution

  • You can create a Job using DBMS_SCHEDULER:

    BEGIN
      -- Job defined entirely by the CREATE JOB procedure.
      DBMS_SCHEDULER.create_job (
        job_name        => 'YourJobName',
        job_type        => 'PLSQL_BLOCK',
        job_action      => 'BEGIN NULL; /* Your code here*/ END;',
        start_date      => SYSTIMESTAMP, // run the job as soon as possible
        repeat_interval => NULL,         // don't repeat the job
        end_date        => NULL,         // ignore this if its not a repeating job
        enabled         => TRUE,         
        comments        => 'Your comments on and description of the job.');
    END;
    /
    

    You can also change job_type to call stored procedures or external executables as well as PL/SQL blocks.

    If you want to view your currently running jobs you can use:

    SELECT JOB_NAME, STATE
    FROM   USER_SCHEDULER_JOBS;
    

    If you want to view a log of your jobs then you can use:

    SELECT *
    FROM   USER_SCHEDULER_JOB_LOG
    ORDER BY LOG_DATE DESC;
    

    and:

    SELECT *
    FROM   USER_SCHEDULER_JOB_RUN_DETAILS
    ORDER BY LOG_DATE DESC;