2014-11-06 81 views
0

我有一個自定義類型中:與自定義類型工作的存儲過程

CREATE OR REPLACE TYPE my_type IS OBJECT 
(
    field_one number, 
    field_two varchar2(10), 
); 

和這種類型的嵌套表:

CREATE TYPE my_type_nt AS TABLE OF my_type; 

,幷包含此嵌套表另一個自定義類型:

CREATE OR REPLACE TYPE parent IS OBJECT 
(
    field_one number, 
    field_two my_type_nt, 
); 

我必須查詢父對象的表,然後根據該記錄的PK,查詢anot她的表爲該父級的所有my_type對象。

所以像:

-- i know following code is wrong 
select * into parent1 
from table1 
where table1.column1 = something; 

然後:

for every record in parent1 
    populate it's my_type_nt 
    for every record in my_type_nt 
     do something 
    end loop 
end loop 

我的問題是:1。 是我的方法不對?我應該加入兩張桌子嗎? 2.我將不得不填充父類型(這存儲proc飼料到另一個父類型作爲輸入的存儲過程。什麼是有效的方式來選擇數據到父類型?

回答

1

我們可以填充嵌套表與子查詢中使用CAST()和MULTISET()

select parent(p.id, 
    cast(multiset(select c.c_id 
         , c.c_name 
       from c 
       where c.p_id = p.id) 
    as my_type_nt) 
    ) 
into local_par 
from p 
where p.id = param_id; 

這是否是最適合你的方法取決於你在你的僞代碼混淆什麼實際的處理:populate it's my_type_nt ... do something

「什麼是有效的方法選擇數據到母型「

如果要處理多個家長,你應該爲它創建一個嵌套表類型太:

CREATE TYPE parent_nt AS TABLE OF parent_t; 
/

然後你可以用批量填充它COLLECT語法:

select parent(p.id, 
    cast(multiset(select c.c_id 
         , c.c_name 
       from c 
       where c.p_id = p.id) 
    as my_type_nt) 
    ) 
bulk collect into local_par_nt 
from p 
where p.id <= param_id; 

然後循環通過收集處理每個父

for idx in 1 .. local_par_nt.count() 
loop 
    do_something; 
相關問題