好的,我有一個複雜的遞歸問題。我想在我的Oracle 11g數據庫中獲得所有對象(all_objects表)的依賴性安裝序列。獲取Oracle對象的安裝順序
首先,我創建了使用此函數保持
我已經嘗試做concate所有依賴的所有依賴
create or replace
view REALLY_ALL_DEPENDENCIES as
select *
from ALL_DEPENDENCIES
union
select owner, index_name, 'INDEX', table_owner, table_name, table_type, null, null
from all_indexes
union
select p.owner, p.table_name, 'TABLE', f.owner, f.table_name, 'TABLE', null, null
from all_constraints p
join all_constraints f
on F.R_CONSTRAINT_NAME = P.CONSTRAINT_NAME
and F.CONSTRAINT_TYPE = 'R'
and p.constraint_type='P'
;
/
編輯觀點:
create
or replace
function dependency(
i_name varchar2
,i_type varchar2
,i_owner varchar2
,i_level number := 0
,i_token clob := ' ') return clob
is
l_token clob := i_token;
l_exist number := 0;
begin
select count(*) into l_exist
from all_objects
where object_name = i_name
and object_type = i_type
and owner = i_owner;
if l_exist > 0 then
l_token := l_token || ';' || i_level || ';' ||
i_name || ':' || i_type || ':' || i_owner;
else
-- if not exist function recursion is finished
return l_token;
end if;
for tupl in (
select distinct
referenced_name
,referenced_type
,referenced_owner
from REALLY_ALL_DEPENDENCIES
where name = i_name
and type = i_type
and owner = i_owner
)
loop
-- if cyclic dependency stop and shout!
if i_token like '%' || tupl.referenced_name || ':' || tupl.referenced_type || ':' || tupl.referenced_owner || '%' then
select count(*) into l_exist
from REALLY_ALL_DEPENDENCIES
where name = tupl.referenced_name
and type = tupl.referenced_type
and owner = tupl.referenced_owner;
if l_exist > 0 then
return '!!!CYCLIC!!! (' || i_level || ';' || tupl.referenced_name || ':' || tupl.referenced_type || ':' || tupl.referenced_owner || '):' || l_token;
end if;
end if;
-- go into recursion
l_token := dependency(
tupl.referenced_name
,tupl.referenced_type
,i_owner /* I just want my own sources */
,i_level +1
,l_token);
end loop;
-- no cyclic condition and loop is finished
return l_token;
end;
/
而且我可以查詢通過
select
object_name
,object_type
,owner
,to_char(dependency(object_name, object_type, owner)) as dependecy
from all_objects
where owner = 'SYSTEM'
;
好的,也許它就像「作弊」,但你不能在創建時做循環依賴。所以至少作爲一個人,我只能創建一個接一個的對象:-)這個序列應該是「逆向工程能力」。
現在我對解決方案比以前更感興趣;-)它仍然是關於棘手的部分......「我怎樣才能從架構順序安裝序列中選擇所有的元素(使用之前的依賴對象列表目的)」? 這只是某種排序問題,不是嗎?
既然你可以有循環依賴,這是不可能的。 – 2010-10-21 18:06:39
是的,但正如你所看到的,我能夠檢測到它們。因此,然後讓我們添加一個約束 - >通過依賴關係排序所有源,並將循環源排序到結果集中的最後。現在有可能得到一個解決方案;-)好吧,mybe你不想這樣做,因爲它對你沒有意義,但它仍然對我有意義:-) – christian 2010-10-22 08:12:42