http://www.cnblogs.com/tohen/archive/2008/11/17/1335186.html
一、在PLSQL中创建表:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
create
table
HWQY.TEST(CARNO
VARCHAR2
(
30
),CARINFOID
NUMBER
)
二、在PLSQL中创建存储过程:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
create
or
replace
procedure
pro_test
AS
carinfo_id
number
;
BEGIN
select
s_CarInfoID.nextval
into
carinfo_id
from
dual;
insert
into
test(test.carno,test.carinfoid)
values
(carinfo_id,
'
123
'
);
commit
;
end
pro_test;
三、在SQL命令窗口中启动任务:
在SQL>后执行:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
VARIABLE jobno
number
;
begin
DBMS_JOB.SUBMIT(:jobno,
'
pro_test;
'
,SYSDATE,
'
sysdate+1/24/12
'
);
commit
;
end
;
/
四、跟踪任务的情况(查看任务队列):
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
SQL
>
select
job,next_date,next_sec,failures,broken
from
user_jobs;
JOB NEXT_DATE NEXT_SEC FAILURES BROKEN
--
-------- ----------- ---------------- ---------- ------
1
2008
-
2
-
22
?
01
:
00
:
00
0
N
说明有一个任务存在了。
执行 select * from test t 查看定时任务的结果。可以看出定时任务是正常执行了的。
五、停止已经启动的定时任务:
先执行 select job,next_date,next_sec,failures,broken from user_jobs; 以查看定时任务的job号。
在SQL>中执行下面的语句停止一个已经启动的定时任务:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
begin
dbms_job.remove(
1
);
commit
;
end
;
/
表示停止job为1的任务。
执行后显示如下:PL/SQL procedure successfully completed
六、查看进程数:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
show parameter job_queue_processes;
--
必须>0,否则执行下面的命令修改:
alter
system
set
job_queue_processes
=
5
;
七、再创建一个任务(每5分钟执行一次):
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
variable jobno
number
;
begin
dbms_job.submit(:jobno,
'
pro_test;
'
,sysdate,
'
sysdate+1/24/12
'
);
commit
;
end
;
/
建立一个定时任务后,在PLSQL中查看JOB,它的 sql语句类似的是如下:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
begin
sys.dbms_job.submit(job
=>
:jobno,
what
=>
'
pro_test;
'
,
next_date
=>
to_date(
'
21-02-2008 17:37:26
'
,
'
dd-mm-yyyy hh24:mi:ss
'
),
interval
=>
'
sysdate+1/24/12
'
);
commit
;
end
;
/
所以,创建一个任务的完整的格式是:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
variable jobno
number
;
begin
sys.dbms_job.submit(job
=>
:jobno,
what
=>
'
pro_test;
'
,
next_date
=>
to_date(
'
21-02-2008 17:37:26
'
,
'
dd-mm-yyyy hh24:mi:ss
'
),
interval
=>
'
sysdate+1/24/12
'
);
commit
;
end
;
/
--
系统会自动分配一个任务号jobno。
八、 执行结果:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
select
job,next_date,next_sec,failures,broken
from
user_jobs;
JOB NEXT_DATE NEXT_SEC FAILURES BROKEN
1
1
2008
-
2
-
22
AM
01
:
00
:
00
01
:
00
:
00
0
N
2
2
2008
-
2
-
21
PM
05
:
42
:
45
17
:
42
:
45
0
N
3
3
2008
-
2
-
21
PM
05
:
42
:
45
17
:
42
:
45
0
N