2011-02-16 34 views
2

我有一個過程,接受類似於下面顯示的parent_arr的輸入作爲通過ODP.Net從應用程序層的輸入。在該過程的第一步中,我將數組中的數據存儲在全局臨時表中,因此我可以使用set邏輯而不是pl/sql循環執行以下幾個步驟。只要數組只有parent_typ的一個成員,一切都很好。但是,當有多個成員時,我得到ORA-01427,單行查詢返回多行。下面的查詢返回兩個集合。我需要在顯示child.name和child.value的單個sql語句中取消兩個集合的嵌套。如何做到這一點?如何在查詢中取消嵌套表的集合?

採樣對象

create type child_typ is object(name varchar2(100), value number); 
create type child_arr is table of dropme_child_typ; 
create type parent_typ is object(pname varchar2(100), child dropme_child_arr); 
create type parent_arr is table of dropme_parent_typ; 

查詢下面會拋出ORA-01427

select * from table(
    select child 
    from table(parent_arr(
     parent_typ('TEST1', 
      child_arr(
       child_typ('C1', 1), 
       child_typ('C2', 2))), 
     parent_typ('TEST2', 
      child_arr(
       child_typ('C3', 3), 
       child_typ('C4', 4)))))); 

此查詢的工作,但返回的對象child_arr的列

select child 
from table(parent_arr(
    parent_typ('TEST1', 
     child_arr(
      child_typ('C1', 1), 
      child_typ('C2', 2))), 
    parent_typ('TEST2', 
     child_arr(
      child_typ('C3', 3), 
      child_typ('C4', 4))))); 

查詢失敗原因我無法訪問「孩子」中的值

select child.name, child.value from 
table(parent_arr(
    parent_typ('TEST1', 
     child_arr(
      child_typ('C1', 1), 
      child_typ('C2', 2))), 
    parent_typ('TEST2', 
     child_arr(
      child_typ('C3', 3), 
      child_typ('C4', 4))))); 

請告訴我,有一種方法可以在不使用pl/sql循環的情況下執行此操作(這是迄今爲止唯一能夠成功實現的方法)。速度是最重要的。我嘗試使用forall語句來遍歷parent_arr的成員,但它會拋出批量綁定錯誤。

回答

1

你可以使用一個lateral join到UNNEST你的孩子對象:

SQL> WITH my_data AS (
    2  SELECT pname, child 
    3  FROM TABLE(parent_arr(parent_typ('TEST1', 
    4          child_arr(child_typ('C1', 1), 
    5             child_typ('C2', 2))), 
    6        parent_typ('TEST2', 
    7          child_arr(child_typ('C3', 3), 
    8             child_typ('C4', 4))))) 
    9 ) 
10 SELECT my_data.pname, child.name, child.value 
11 FROM my_data, table(my_data.child) child; 

PNAME NAME  VALUE 
-------- ----- ---------- 
TEST1 C1    1 
TEST1 C2    2 
TEST2 C3    3 
TEST2 C4    4 

這是外連接,您加入其子女的父或母的形式。

+0

作品。非常感謝。我正在追求演員(multiset)(...今天早上,我想這肯定會起作用 – 2011-02-16 11:53:03