Job特性的学习
Job的设置问题,非常初级的内容。想想还是记下来,以备到今后查询之用。
1、创建Job
variable jobno number;
begin
sys.dbms_job.submit(job => :jobno,
what => 'your_procedure1;
your_procedure2;',
next_date => to_date('20080918 18:50:00', 'yyyymmdd hh24:mi:ss'),
interval => 'sysdate+1/24');
commit;
end;
/
2、删除Job
print jobno;
begin
dbms_job.remove(:jobno);
commit;
end;
/
3、其他操作:
修改要执行的操作:dbms_job.what(jobno,what);
修改下次执行时间:dbms_job.next_date(job,next_date);
修改间隔时间:dbms_job.interval(job,interval);
停止job:dbms.broken(job,broken,nextdate);
启动job:dbms_job.run(jobno);
修改job_queue_processes的值:
可通过select * from v$parameter;
查看其值;
或者直接用show parameter job_queue_processes;
查看如下:
NAME TYPE VALUE
--------------- ----------- ------------
job_queue_processes integer 10
方法1.startup pfile='C: oracleora90databaseinitorcl.ora';
//这个方法用来修改initorcl.ora文件的job_queue_processes参数,然后重新启动数据库
方法2.alter system set job_queue_processes=10
//这个方法不用重启数据库就可以生效,系统自动修改init.ora文件以后即可生效 。
注意:保证参数不为0,否则JOB不自动运行
4、时间间隔偏移的问题:
SQL> create table t10 (a int ,b date);
Table created
SQL> create or replace procedure k
2 as
3 i int := 0;
4 begin
5 insert into t10 values(i,sysdate);
6 commit;
7 end;
8 /
Procedure created
SQL> begin
2 sys.dbms_job.submit(job => :jobno,
3 what => 'k;',
4 next_date => to_date(sysdate),
5 interval => 'sysdate+1/1440');
6 commit;
7 end;
8 /
PL/SQL procedure successfully completed
jobno
---------
47
SQL> column B format a30
SQL> column A format a5
SQL> select * from t10;
A B
----- ------------------------------
0 2008-9-18 17:07:38
0 2008-9-18 17:08:39
0 2008-9-18 17:09:40
0 2008-9-18 17:10:41
发现虽然设置了每分钟执行一次,但是每次执行时间是在向后偏移
所以需要进行调整,将job建为如下:
SQL> begin
2 dbms_job.remove(:jobno);
3 commit;
4 end;
5 /
PL/SQL procedure successfully completed
jobno
---------
47
SQL> begin
2 sys.dbms_job.submit(job => :jobno,
3 what => 'k;',
4 next_date => to_date(sysdate),
5 interval => 'trunc(sysdate,''mi'')+1/1440');
6 commit;
7 end;
8 /
PL/SQL procedure successfully completed
jobno
---------
48
SQL> truncate table t10;
Table truncated
SQL> select * from t10;
A B
----- ------------------------------
0 2008-9-18 17:19:02
0 2008-9-18 17:20:03
0 2008-9-18 17:21:04
0 2008-9-18 17:22:01
0 2008-9-18 17:23:02
虽然秒数还是有稍微的不同,这是由于每次job的运行时间所致
但是不会像之前一样一直往后偏移。