在Oracle SQL中重新编译无效对象

时间:2013-10-04 08:40:33

标签: sql oracle

作为数据库构建的一部分,我们有一些sql重新编译数据库中的所有无效对象。这工作正常,但现在我们看到以下错误:

ORA-04047: object specified is incompatible with the flag specified
ORA-06512: at "SYS.DBMS_UTILITY", line 156
ORA-06512: at "SYS.DBMS_DDL", line 157
ORA-06512: at line 51

这是否与我们刚刚为基于版本的重新定义创建了一系列版本视图这一事实有关?

以下是代码:

DECLARE

v_counter INTEGER := 1;

CURSOR cur_invalid_objects
IS
SELECT object_name,
       object_type
  FROM user_objects
 ---*** exclude VIEW and MATERIALIZED VIEW object types
  WHERE status = 'INVALID'
  and object_type NOT IN ('VIEW','MATERIALIZED VIEW');



FUNCTION get_total_invalid_objects
RETURN INTEGER
IS

 CURSOR get_total_invalid_objs_cur
 IS
 SELECT count(*)
   FROM user_objects
  ---*** exclude VIEW and MATERIALIZED VIEW object types
    WHERE status = 'INVALID'
   AND object_type NOT IN ('VIEW','MATERIALIZED VIEW');

 v_total INTEGER := 0;

BEGIN
 OPEN get_total_invalid_objs_cur;
FETCH get_total_invalid_objs_cur 
 INTO v_total;
CLOSE get_total_invalid_objs_cur;

RETURN v_total;

END get_total_invalid_objects;  


BEGIN
WHILE get_total_invalid_objects > 0
LOOP
  IF v_counter <= 100 -- Failsafe: exit while loop if the objects cannot be recompiled after 100 tries
  THEN
     FOR cur_invalid_objects_rec IN cur_invalid_objects 
     LOOP
    EXIT WHEN cur_invalid_objects%NOTFOUND;
        dbms_ddl.alter_compile(cur_invalid_objects_rec.object_type,NULL,cur_invalid_objects_rec.object_name);
     END LOOP;
     v_counter := v_counter + 1;
  ELSE
     dbms_output.put_line('Unable to recompile objects to a status of: VALID.  Please investigate further.');
     EXIT; 
  END IF;

END LOOP;

END;
/
EXIT
/

我尝试删除VIEW的排除条款,但我也遇到了同样的错误。

2 个答案:

答案 0 :(得分:0)

DBMS_DDL.ALTER_COMPILE支持的对象类型包括PROCEDURE,FUNCTION,PACKAGE,PACKAGE BODY和TRIGGER(参见Oracle documentation)。

也许你的同义词无效。尝试将查询限制为这些类型。

顺便说一下,还有DBMS_UTILITY.compile_schema(如a_horse_with_no_name所述)

答案 1 :(得分:0)

也许这也有帮助: UTL_RECOMP