Oracle in the job (cron job)

The oracle Job (scheduled tasks) implemented by the function in dbms_job packet.
Theoretical knowledge job can refer https://blog.csdn.net/apextrace/article/details/7767506

A, dbms_job related to knowledge

1. Create a job:
   variable jobno number;
   dbms_job.submit(:jobno, —-job号 
                   'Your_procedure;', - stored procedure to execute, ';' can not be omitted 
                   next_date, - the next execution time 
                   'Interval' - each time interval, interval in days
                   );
   - The system will automatically assign a task number jobno.
2、删除job: dbms_job.remove(jobno);
3, job modification operation to be performed: dbms_job.what (jobno, what); --what new operating (stored procedure)
4, modify the next execution time: dbms_job.next_date (jobno, next_date);
5, modified interval: dbms_job.interval (jobno, interval);
6, start the job: dbms_job.run (jobno);
7、停止job: dbms_job.broken(jobno, broken, nextdate); –broken为boolean值

Second, the initialization parameters job_queue_processes

1, job_queue_process oracle can be represented by the number of concurrent job, when job_queue_process value of 0 indicates the oracle job are stopped.
2, View job_queue_processes parameters
   method one:
         show parameter job_queue_process;
   Method Two:
         select * from v$parameter where name='job_queue_processes';
3, modify parameters job_queue_processes
   alter system set job_queue_processes = 10;

Three, user_jobs table structure

Fields (columns) Type Description
No job number that uniquely identifies the task
log_user varchar2 (30) submitted by the user's tasks
priv_user varchar2 (30) given the mandate of user
schema_user varchar2 (30) to task for user mode parsed
last_date date of the last successful run of the task time
(8) hh24 last_sec varchar2: mm: ss format Date last_date hours, minutes, and seconds
this_date date start time is running task, the task was null if not running
this_sec varchar2 (8) as hh24: mm: ss format Date this_date hours, minutes, and seconds 
of time to run the task in a regular next_date date

Fourth, example

1, create a table in plsql in:
create table t(
 id varchar2(30),
 name varchar2(30)
);

2, create a stored procedure in the plsql:
create or replace procedure proce_t is
begin
 insert into t(id, name) values('1', to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'));
 commit;
end proce_t;
/

3, create a job task (one minute executed once):
In sql> after execution:
variable jobno number;
begin
 dbms_job.submit(:jobno,'proce_t;', sysdate, 'sysdate+1/24/60');
 commit;
end;
/
After submitting tips:
pl/sql procedure successfully completed
jobno
---------
25

4, tracking tasks (see task queue):
sql> select job, next_date, next_sec, failures, broken from user_jobs;
  
 job next_date next_sec failures broken
---------- ----------- ---------------- ---------- ------
 25 2012/9/14 1 10:59:46 0 n
Description task has been created successfully.
Execute select * from t; see the scheduled tasks results. It can be seen that the timing of the tasks are performed normally.

5, stop the timer task
(1) View timed task of job numbers.
sql> select job, next_date, next_sec, failures, broken from user_jobs; 
  
 job next_date next_sec failures broken
---------- ----------- ---------------- ---------- ------
 25 2012/9/14 1 11:01:48 0 n
(2) to stop a scheduled task has been started:
begin
 dbms_job.broken(25, true, sysdate);
 commit;
end;
/
Means stop job for the task 25.

After the display is performed as follows:
pl/sql procedure successfully completed
(3) Check timer task has stopped successfully
sql> select job, next_date, next_sec, failures, broken from user_jobs; 
  
 job next_date next_sec failures broken
---------- ----------- ---------------- ---------- ------
 4000/1/1 00:00:00 0 and 25
broken value y, a timing task is stopped.

6, starting a scheduled task
(1) stop a scheduled task View
sql> select job, next_date, next_sec, failures, broken from user_jobs; 
  
 job next_date next_sec failures broken
---------- ----------- ---------------- ---------- ------
 4000/1/1 00:00:00 0 and 25
broken value y, a timing task is stopped.
(2) start timer task
begin
 dbms_job.run(25);
 commit;
end;
/
(3) View scheduled tasks are started
sql> select job, next_date, next_sec, failures, broken from user_jobs;
  
 job next_date next_sec failures broken
---------- ----------- ---------------- ---------- ------
 25 2012/9/14 1 11:06:17 0 n
broken is n, a timing to start the task successfully.

7, see the number of processes
show parameter job_queue_processes;
It must be greater than 0, otherwise execute the following command to change:
alter system set job_queue_processes=10;

8, and then create a task (performed once every 5 minutes):
variable jobno number;
begin
 dbms_job.submit (: jobno, 'proce_t;', sysdate, 'sysdate + 1/24/12'); --interval units in days
 commit;
end;
/

9, execution
select job,next_date,next_sec,failures,broken from user_jobs;
result:
sql> select job,next_date,next_sec,failures,broken from user_jobs; 
  
 job next_date next_sec failures broken
---------- ----------- ---------------- ---------- ------
 26 2012/9/14 1 11:12:08 0 n
 25 2012/9/14 1 11:07:18 0 n

V. Summary

About job running time
(1) executed per minute
Interval => TRUNC(sysdate,'mi') + 1/(24*60)
(2) Timing execution day
For example: daily 1:00 execution
Interval => TRUNC(sysdate) + 1 +1/(24)
(3) Timing execution week
For example: Monday 1:00 execution
Interval => TRUNC (next_day (sysdate, 'Monday')) + 1/24
(4) Timing execution month
For example: every month at 1:00 on the 1st execution
Interval =>TRUNC(LAST_DAY(SYSDATE))+1+1/24
(5) the timing of execution quarterly
For example, the first day of each quarter 1:00 execution
Interval => TRUNC(ADD_MONTHS(SYSDATE,3),'Q') + 1/24
(6) Timing execution every six months
For example: the annual July 1 and at 1:00 on January 1
Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),6)+1/24
(7) the timing of the implementation of the annual
For example: Every year at 1:00 on January 1 execution
Interval =>ADD_MONTHS(trunc(sysdate,'yyyy'), 12)+1/24

job的运行频率设置
(1)每天固定时间运行,比如早上8:10分钟:Trunc(Sysdate+1) + (8*60+10)/24*60
(2)Toad中提供的:
每天:trunc(sysdate+1)
每周:trunc(sysdate+7)
每月:trunc(sysdate+30)
每个星期日:next_day(trunc(sysdate),'星期日')
每天6点:trunc(sysdate+1)+6/24
半个小时:sysdate+30/(24*60)
每个小时的第15分钟运行,比如:8:15,9:15,10:15…:trunc(sysdate,'hh')+(60+15)/(24*60) 

  

转自:https://www.jb51.net/article/92575.htm

Guess you like

Origin www.cnblogs.com/shujk/p/12541676.html