2015-11-13 30 views
0

我不知道很熱以在查詢語句中得到一個收集類型。 例如在SQL查詢中返回一個收集類型

我定義我喜歡的類型,所有...

當我這樣做:

SELECT 
    MyType(att1,att2) 
    FROM 
    table 

其返回

| ATT1 | att2 |

| a | b |

| c | d |

。 。

當我這樣做:

SELECT 
    MyTABLE_COLLECT_TYPE (MyType(att1,att2)) 
    FROM 
    table 

其返回

| MyTABLE_COLLECT_TYPE |

| collection |

| collection |

。 。

而且每個集合有一個記錄的MyType(ATT1,ATT2)

所以我questio是我怎麼能得到的只有一個集合中的所有類型的MyType的(ATT1,ATT2),單行返回整個表。 (我不想使用的功能,如果可能的:P)

謝謝:)

回答

3

你需要使用表(t.collection_field)

看到鏈接Unnesting Results of Collection Queries

例子以上:

SELECT e.* 
    FROM department_persons d, TABLE(d.dept_emps) e; 

IDNO NAME PHONE 
---------- ------------------------------ --------------- 
1 John Smith 1-650-555-0135 
2 Diane Smith 1-650-555-0135 

從上方

鏈路擴展樣本
CREATE TYPE person_typ AS OBJECT (
    idno   NUMBER, 
    name   VARCHAR2(30), 
    phone   VARCHAR2(20), 
    MAP MEMBER FUNCTION get_idno RETURN NUMBER, 
    MEMBER PROCEDURE display_details (SELF IN OUT NOCOPY person_typ)); 

CREATE TYPE BODY person_typ AS 
    MAP MEMBER FUNCTION get_idno RETURN NUMBER IS 
    BEGIN 
    RETURN idno; 
    END; 
    MEMBER PROCEDURE display_details (SELF IN OUT NOCOPY person_typ) IS 
    BEGIN 
    -- use the put_line procedure of the DBMS_OUTPUT package to display details 
    DBMS_OUTPUT.put_line(TO_CHAR(idno) || ' - ' || name || ' - ' || phone); 
    END; 
END; 


CREATE TYPE people_typ AS TABLE OF person_typ; -- nested table type 


CREATE TABLE department_persons (
    dept_no NUMBER PRIMARY KEY, 
    dept_name CHAR(20), 
    dept_mgr person_typ DEFAULT person_typ(10,'John Doe',NULL), 
    dept_emps people_typ DEFAULT people_typ()) -- instance of nested table type 
    NESTED TABLE dept_emps STORE AS dept_emps_tab; 

INSERT INTO department_persons VALUES 
    (101, 'Physical Sciences', person_typ(65,'Vrinda Mills', '1-650-555-0125'), 
      people_typ(person_typ(1, 'John Smith', '1-650-555-0135'), 
         person_typ(2, 'Diane Smith', NULL))); 
INSERT INTO department_persons VALUES 
    (104, 'Life Sciences', person_typ(70,'James Hall', '1-415-555-0101'), 
    people_typ()); -- an empty people_typ table 

select * from department_persons 回報收藏在你的描述

DEPT_NO DEPT_NAME DEPT_MGR.IDNO DEPT_MGR.NAME DEPT_MGR.PHONE DEPT_EMPS 
1 101 Physical Sciences  65 Vrinda Mills 1-650-555-0125 <Collection> 
2 104 Life Sciences   70 James Hall 1-415-555-0101 <Collection> 

如果添加你有你需要什麼

SELECT e.* 
    FROM department_persons d, TABLE(d.dept_emps) e; 

    IDNO NAME PHONE 
1 1 John Smith 1-650-555-0135 
2 2 Diane Smith