10

I have a problem, when I try to compile my project in Oracle Database. To make it more simple, I have three objects: 2 packages (UTILS and TYPES) and 1 view (VIEW).

Package UTILS is using types defined in package TYPES. Package TYPES is using VIEW as a base for one of it's types. And VIEW is using functions from package UTILS in it's script. When I try to make some changes to one of these objects, I can't compile because everything is in invalid state. So some kind of object dependency loop is created.

Please help me to resolve this issue.

For example, is there anyway to compile the below code? Each object is individually syntactically correct, but how can they all be compiled together?

create or replace package my_types is
   type type1 is table of number;
   type type2 is table of my_view%rowtype;
end;
/

create or replace package my_utils is
   function get_1 return number;
   procedure do_something(parameter my_types.type2);
end;
/

create or replace package body my_utils is
   function get_1 return number is
   begin
       return 1;
   end;

   procedure do_something(parameter my_types.type2) is
   begin
       null;
   end;
end;
/

create or replace force view my_view as
select * from dual
where 1 = my_utils.get_1();

exec dbms_utility.compile_schema(user, false);

select object_name from user_objects where status <> 'VALID';
4

3 回答 3

3

如果你在两个视图中打破视图,你可以打破循环依赖:

create or replace view my_view_1
as select * from dual; 

create or replace package my_types is
   type type1 is table of number;
   type type2 is table of my_view_1%rowtype;
end;
/

create or replace package my_utils is
   function get_1 return number;
   procedure do_something(parameter my_types.type2);
end;
/

create or replace package body my_utils is
   function get_1 return number is
   begin
       return 1;
   end;

   procedure do_something(parameter my_types.type2) is
   begin
       null;
   end;
end;
/

create or replace view my_view as
select * from my_view_1
where 1 = my_utils.get_1();

编辑:另一种可能性是将包 my_utils 分成两个:

create or replace package my_utils_1 is
   function get_1 return number;
end;
/
create or replace package body my_utils_1 is
   function get_1 return number is
   begin
       return 1;
   end;
end;
/

create or replace view my_view as
select * from dual
where 1 = my_utils_1.get_1();

create or replace package my_types is
   type type1 is table of number;
   type type2 is table of my_view%rowtype;
end;
/

create or replace package my_utils_2 is
   procedure do_something(parameter my_types.type2);
end;
/
create or replace package body my_utils_2 is
   procedure do_something(parameter my_types.type2) is
   begin
       null;
   end;
end;
/
于 2013-09-10T22:40:32.097 回答
2

我会避免使用打包类型和 %ROWTYPE。这些不是标准 SQL,可以用结构化类型替换

create or replace view my_view_1
as select * from dual; 

create or replace type type1 as table of number;
create or replace type type2 as object (DUMMY VARCHAR2(1 byte));
create or replace type table_type2 as table of type2;

create or replace package my_utils is
   function get_1 return number;
   procedure do_something(parameter table_type2);
end;
/

create or replace package body my_utils is
   function get_1 return number is
   begin
       return 1;
   end;

   procedure do_something(parameter table_type2) is
   begin
       null;
   end;
end;
/

create or replace view my_view as
select * from my_view_1
where 1 = my_utils.get_1();
于 2013-09-12T13:08:29.180 回答
1

如果你不想/不能拆分你的包或视图,你总是可以先创建一个虚拟版本的视图,编译包,然后创建“真实”视图:

create or replace package my_types is
   type type1 is table of number;
   type type2 is table of my_view%rowtype;
end;
/

create or replace package my_utils is
   function get_1 return number;
   procedure do_something(parameter my_types.type2);
end;
/

create or replace package body my_utils is
   function get_1 return number is
   begin
       return 1;
   end;

   procedure do_something(parameter my_types.type2) is
   begin
       null;
   end;
end;
/

create or replace force view my_view as
select * from dual;

exec dbms_utility.compile_schema(user, false);

create or replace force view my_view as
select * from dual
where 1 = my_utils.get_1();

select object_name from user_objects where status <> 'VALID';
于 2013-09-11T11:03:44.623 回答