首页 » ORACLE 9i-23c » oracle procedure等对象失效后编译问题

oracle procedure等对象失效后编译问题

如果procedure 所使用的表结构发生了改变等其它情况,在相应的xxx_objects表的status字段会变为invalid状态,但是如果在调用时procedure会自动编译,grant失效对象权限给别人时也会自动编译,也是在dba_objects表可以查到最后一次编译的时间last_ddl_time,但并不是说调用就会重编译,

高并发的oltp , 多个session 同时编译 就会出现 library cache lock/pin,最好还是更新完,手动编译.

对于已失效的存储过程,只要不是语法上的问题,编译一下又可以重新使用了,手动编译而不等到用时也要做到自动化,方法如下

1. Oracle SQL *Plus 中 —— 用 spool 生成脚本文件,然后 @ 调入执行,代码如下:

spool comProc.sql

select ‘alter procedure ‘||object_name||’ compile;’ from all_objects
where status = ‘INVALID’ and object_type = ‘PROCEDURE’ AND owner=’xxx’;

spool off

@comProc.sql;

2. 写成一个存储过程 —— 让这个存储过程在某个时机执行,比如  Job 中,代码如下:

create or replace procedure compile_invalid_procedures(
p_owner varchar2 — 所有者名称,即 SCHEMA
) as

–编译某个用户下的无效存储过程

str_sql varchar2(200);

begin
for invalid_procedures in (select object_name from all_objects
where status = ‘INVALID’ and object_type = ‘PROCEDURE’ and owner=upper(p_owner))
loop
str_sql := ‘alter procedure ‘ ||invalid_procedures.object_name || ‘ compile’;
begin
execute immediate str_sql;
exception
–When Others Then Null;
when OTHERS Then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;

编译失效的其它对象

alter 的语句写法参考下面:

alter function function_name compile;
alter package package)name compile;
alter type type_name compile;
alter index index_name rebuild;
–等等…………

可以用 Oracle 提供的工具:dbms_utility.compile_schema(schema varchar2, compile_all boolean default TRUE);来编译方案下的所有对象。

打赏

目前这篇文章有3条评论(Rss)评论关闭。

  1. tab mcaally | #1
    2011-01-25 at 19:43

    Can you provide more information on this? cheers

  2. college grants | #2
    2011-01-15 at 09:46

    It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

    • Betti | #3
      2011-06-16 at 19:54

      Posts like this brgihetn up my day. Thanks for taking the time.