日期:2014-05-16  浏览次数:20546 次

Oracle之Job应用

最近写服务,服务上线后,需要写一个定时执行的SQL脚本,清理并更新数据库表里的数据,应用到了Oracle 的 Job的相关知识。在此总结一下。

?

一:查看相关job信息

? ?1、相关视图?

dba_jobs?

all_jobs?

user_jobs?

dba_jobs_running 包含正在运行job相关信息。

?

? ? ?select * from dba_jobs ;

?

? ? ?--dba_jobs_running 包含正在运行job相关信息

select * from dba_jobs_running;

?

?

2、运行JOB?

说明:Run()过程用来立即执行一个指定的工作。这个过程只接收一个参数:?

SQL> begin?

2 ?dbms_job.run(:job);?

3 ?end;?

4 ?/?

?

----------------------------------------------

在plSQL中我的做法是:

begin

dbms_job.run(3017);

end;?

----------------------------------------------

?

3、删除JOB?

SQL> begin?

2 ?dbms_job.remove(:job);--:job可以用dba_jobs.job的值代替如:1198?

3 ?end;?

4 ?/?

?

二:参数:job_queue_process

?

pl/sql的命令窗口执行:

SQL> show parameter job_queue_process;

?

NAME ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TYPE ? ? ? ?VALUE

------------------------------------ ----------- ------------------------------

job_queue_processes ? ? ? ? ? ? ? ? ?integer ? ? 10

?

通过show parameter job_queue_process 来查看oracle中 job_queue_process的值。 当job_queue_process的值为0时表示全部停止oracle的job。

可以通过语句: ALTER SYSTEM SET job_queue_processes = 10; ?来调整启动oracle的job。

?

视图dba_jobs字段的含义:

?

? ?JOB ? 任务的唯一标示号

? ? ? ? ?Identifier of job. Neither import/export nor repeated executions change it.

?

? ?LOG_USER ? ? 提交任务的用户

? ? ? ? ? ? ? ? USER who was logged in when the job was submitted

? ?PRIV_USER ? ?赋予任务权限的用户

? ? ? ? ? ? ? ? USER whose default privileges apply to this job

? ?SCHEMA_USER ?对任务作语法分析的用户模式

? ? ? ? ? ? ? ? select * from bar means select * from schema_user.bar

? ?LAST_DATE ? ?最后一次成功运行任务的时间

? ? ? ? ? ? ? ? Date that this job last successfully executed

? ?LAST_SEC ? ? 如HH24:MM:SS格式的last_date日期的小时,分钟和秒

? ? ? ? ? ? ? ? Same as LAST_DATE. This is when the last successful execution started.

? ?THIS_DATE ? ?正在运行任务的开始时间,如果没有运行任务则为null

? ? ? ? ? ? ? ? Date that this job started executing (usually null if not executing)