在sql查询中返回一个collect类型

时间:2015-11-13 20:36:00

标签: sql oracle plsql

我不知道如何在查询中获得收集类型。 例如

我定义了我的类型和所有......

当我这样做时:

   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)

所以我的问题是我如何才能获得所有类型MyType(att1,att2)的一个集合,一行返回整个表。 (如果可能,我不想使用函数:P)

谢谢:)

1 个答案:

答案 0 :(得分:3)

您需要使用 TABLE(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>

如果你添加 TABLE ,你就拥有了你需要的东西

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