工作中可能遇到这样的情况,在A用户下有一个不用的job,但是dba不知道A用户的密码,怎么删除这个job呢。
相信大部分人都会尝试在sys用户下用dbms_job.remove()命令去删除它,但是会报错
sys用户下
SQL> exec dbms_job.remove(70);
BEGIN dbms_job.remove(70); END;
*
ERROR at line 1:
ORA-23421: job number 70 is not a job in the job queue
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 86
ORA-06512: at "SYS.DBMS_IJOB", line 680
ORA-06512: at "SYS.DBMS_JOB", line 174
ORA-06512: at line 1
还是在sys用户下,使用dbms_ijob.remove()就可以删除
SQL> exec dbms_ijob.remove(70);
PL/SQL procedure successfully completed.
那么,到底dbms_job和dbms_ijob到底怎么用,彼此有什么区别呢?下面用实验证明。
一、sys用户使用dbms_ijob为hou用户创建job
1.查看dbms_ijob.submit的书写格式
sys用户下desc dbms_ijob
PROCEDURE SUBMIT
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
JOB BINARY_INTEGER IN
LUSER VARCHAR2 IN --> Login user when the job was submitted
PUSER VARCHAR2 IN --> User whose default privileges apply to this job
CUSER VARCHAR2 IN --> schema_user
NEXT_DATE DATE IN
INTERVAL VARCHAR2 IN
BROKEN BOOLEAN IN
WHAT VARCHAR2 IN -->procedure
NLSENV VARCHAR2 IN
ENV RAW IN
2.hou用户下创建一个简单存储过程
SQL> conn hou/
Enter password:
Connected.
SQL> create table test(a date);
Table created.
SQL> create or replace procedure pro_insert_date as
begin
insert into test values(sysdate);
end;
/ 2 3 4 5
Procedure created.
3.sys用户为hou创建5号job
SQL> conn / as sysdba
Connected.
SQL> begin
dbms_ijob.submit(5,'HOU','HOU','HOU',
to_date('2012-09-29 12:30:00','YYYY-MM-DD HH24:MI:SS'),'sysdate+1',false,'pro_insert_date;',
'NLS_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_TERRITORY=''CHINA'' NLS_CURRENCY=''¥'' NLS_ISO_CURRENCY=''CHINA'' NLS_NUMERIC_CHARACTERS=''.,'' NLS_DATE_FORMAT=''yyyy-mm-dd hh24:mi:ss'' NLS_DATE_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_SORT=''BINARY''',
'0102000200000000');
commit;
end;
/
匿名块已完成
或者写成
begin
dbms_ijob.submit(5,'HOU','HOU','HOU',
to_date('2012-09-29 12:30:00','YYYY-MM-DD HH24:MI:SS'),'sysdate+1',false,'pro_insert_date;',
'NLS_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_TERRITORY=''CHINA'' '||
'NLS_CURRENCY=''¥'' NLS_ISO_CURRENCY=''CHINA'' '||
'NLS_NUMERIC_CHARACTERS=''.,'' '||
'NLS_DATE_FORMAT=''YYYY-MM-DD HH24:MI:SS'' '||
'NLS_DATE_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_SORT=''BINARY'' ',
'0102000200000000');
commit;
end;
/
注意:1.dbms_ijob.submit创建job时,只能手动指定job号。
2.书写顺序要按照上面的顺序。
3.用户名一定要大写。
注意:上面两条写法是在sql developer中能够执行成功。在sqlplus下无法执行成功,由于涉及到中文货币符号,在这里也就是NLS_CURRENCY=''¥''。
想要在sqlplus下执行成功,将¥换成$即可。至于如何能够在sqlplus下使用中文货币符号,还需要再研究。
4.登录到hou用户查看5号job的情况
SQL> conn hou/
Enter password:
Connected.
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
JOB WHAT LOG_USER PRIV_USER SCHEMA_USE TO_CHAR(LAST_DATE,' TO_CHAR(NEXT_DATE,' INTERVAL B FAILURES
---------- ------------------------------ ---------- ---------- ---------- ------------------- ------------------- -------------------- - ----------
5 pro_insert_date; HOU HOU HOU 2012-09-29 14:42:14 2012-09-30 14:42:14 sysdate+1 N 0
可以看到,sys用户通过dbms_ijob已经为hou用户创建了job。
二、执行5号job
1.在hou用户下
SQL> conn hou
Enter password:
Connected.
SQL> exec dbms_job.run(5);
PL/SQL procedure successfully completed.
2.在sys用户下执行
SQL> conn / as sysdba
Connected.
SQL> exec dbms_ijob.run(5);
PL/SQL procedure successfully completed.
sys通过dbms_ijob.submit为其他用户创建的job,sys和job所在的用户都可以执行。
三、修改job
1.hou用户修改job的执行间隔时间(interval)和下次执行时间(next_day)
SQL> exec dbms_job.interval(5,'sysdate+2');
PL/SQL procedure successfully completed.
查看job情况
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
JOB WHAT LOG_USER PRIV_USER SCHEMA_USE TO_CHAR(LAST_DATE,' TO_CHAR(NEXT_DATE,' INTERVAL B FAILURES
---------- ------------------------------ ---------- ---------- ---------- ------------------- ------------------- -------------------- - ----------
5 pro_insert_date; HOU HOU HOU 2012-09-29 14:45:50 2012-09-30 14:45:50 sysdate+2 N 0
修改next_day
SQL> exec dbms_job.next_date(5,sysdate+1/24);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
2.sys用户
SQL> exec dbms_ijob.interval(5,'sysdate+2');
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
修改next_day
SQL> exec dbms_ijob.next_date(5,sysdate+5);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
四.中断job
1.hou用户
SQL> exec dbms_job.broken(5,true);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL>
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
JOB WHAT LOG_USER PRIV_USER SCHEMA_USE TO_CHAR(LAST_DATE,' TO_CHAR(NEXT_DATE,' INTERVAL B FAILURES
---------- ------------------------------ ---------- ---------- ---------- ------------------- ------------------- -------------------- - ----------
5 pro_insert_date; HOU HOU HOU 2012-09-29 14:45:50 4000-01-01 00:00:00 sysdate+2 Y 0
2.sys用户
SQL> exec dbms_ijob.broken(5,true);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> conn hou
Enter password:
Connected.
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
JOB WHAT LOG_USER PRIV_USER SCHEMA_USE TO_CHAR(LAST_DATE,' TO_CHAR(NEXT_DATE,' INTERVAL B FAILURES
---------- ------------------------------ ---------- ---------- ---------- ------------------- ------------------- -------------------- - ----------
5 pro_insert_date; HOU HOU HOU 2012-09-29 14:45:50 4000-01-01 00:00:00 sysdate+2 Y 0
五.删除job
1.hou用户
SQL> exec dbms_job.remove(5);
PL/SQL procedure successfully completed.
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
no rows selected
2.sys用户
SQL> exec dbms_ijob.remove(5);
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> conn hou
Enter password:
Connected.
SQL> select job,what,LOG_USER,PRIV_USER,SCHEMA_USER,to_char(last_date,'YYYY-MM-DD HH24:MI:SS'),to_char(next_date,'YYYY-MM-DD HH24:MI:SS'),interval,broken,failures from user_jobs;
no rows selected
总结:
1.dbms_job只能在当期用户内创建job、修改和删除job,不能对其他用户的job进行操作;sys用户也无法用dbms_job管理其他用户的job。
2.dbms_ijob只能由sys用户去执行,拥有DBA权限的用户都没有权限去执行它。
3.通过dbms_ijob sys用户可以给其他用户创建job,且job在该用户下,在该用户内可以通过user_jobs视图看到。
4.通过dbms_ijob sys用户能够对其他用户中的job进行删除、修改。
5.sys用户通过dbms_ijob给X用户创建job,那么X用户对该job拥有修改和删除的权限。
dbms_ijob书写格式:
1.创建job
begin
dbms_ijob.submit(jobnu,' LUSER ',' PUSER ',' CUSER ',
next day,'interval',false,'procedure_name;',
'NLS_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_TERRITORY=''CHINA'' '||
'NLS_CURRENCY=''¥'' NLS_ISO_CURRENCY=''CHINA'' '||
'NLS_NUMERIC_CHARACTERS=''.,'' '||
'NLS_DATE_FORMAT=''YYYY-MM-DD HH24:MI:SS'' '||
'NLS_DATE_LANGUAGE=''SIMPLIFIED CHINESE'' NLS_SORT=''BINARY'' ',
'0102000200000000');
commit;
end;
/
2.执行job
exec dbms_ijob.run(jobnumber);
3.修改job的间隔时间(interval)
exec dbms_ijob.interval(5,'sysdate+2');
4.修改job的下次执行时间
exec dbms_ijob.next_date(5,sysdate+5);
5.中断job
exec dbms_ijob.broken(5,true);
6.删除job
exec dbms_ijob.remove(5);