---
title: "oracle定时任务基础"
author: "vortexchoo"
date: "2014-09-10T18:24:04Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2014-09-10T18:24:04Z"
id: 2114991
url: "https://vortexchoo.iteye.com/blog/2114991"
---

以下操作均在PL/SQL中执行

 

创建定时任务：

     创建定时任务的意义就是在设定好的时间对数据库进行操作。

首先我们创建一个过程（根据实际需求）：

create table test(id varchar2(200),val number(4));--创建测试表

insert into test values('001',0);--插入一条测试数据

create or replace prc\_test

is

--此过程操作test表，每30S 进行一次修改操作，操作内容：id为001 的数据 val值+1

update test a set a.val=a.val+1 where a.id = '001';

 

end;

 

 

创建job(任务)

declare     
  n\_job number;  
begin  
DBMS\_JOB.SUBMIT(job =&gt;n\_job,  
    what =&gt;'prc\_test;',   --创建的存储过程名称  
    next\_date =&gt;SYSDATE,  
    interval =&gt;'SYSDATE+30/(60\*24\*60)');  
    commit;  
  end;

 

查询job

select * from user\_jobs;

查询结果中会显示现有任务的信息

获得job（字段）的值

 

 

开始执行任务

declare  
        begin  
          dbms\_job.run(job=&gt;5);--5 就是上述job的值  
        end;

 

 

以上是基本的操作。当然还有对job的

修改

Change (job        IN binary\_integer,   
                  What       IN varchar2,   
                  next\_date  IN date,   
                  interval   IN varchar2)

删除

  declare  
  job\_n number;  
   begin  
   job\_n:=5; --5为上述job值  
        dbms\_job.remove(job=&gt;job\_n);  
        commit;  
end

 

等等。