2015-11-13 63 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