Sql 构造包含嵌套表的对象

Sql 构造包含嵌套表的对象,sql,oracle,plsql,Sql,Oracle,Plsql,我有一个包含一些嵌套表的对象,我想用一个select语句来构造它。我的类型如下所示 create or replace type number_table as table of number; / create or replace type multiple_tables as object ( num_table1 number_table, num_table2 number_table ) 下面展示了我想要做的事情,同样理想的情况是在一条sql语句中避免上下文切换 selec

我有一个包含一些嵌套表的对象,我想用一个select语句来构造它。我的类型如下所示

create or replace type number_table as table of number;
/
create or replace type multiple_tables as object
(
  num_table1 number_table,
  num_table2 number_table
)
下面展示了我想要做的事情,同样理想的情况是在一条sql语句中避免上下文切换

select multiple_tables(number_table
                       (select tblB.purchaseID from table_purchases tblB where tblB.customerID = tblA.customerID),
                       number_table
                       (select tblC.paymentID from table_payments tblC where tblC.customerID = tblA.customerID))
  from table_customer
 where table_customer.customerName = 'John Smith';
这方面的具体错误是ORA-00936:缺少表达式,以防有人怀疑

本质上,是否可以从另一个select语句中构造嵌套表?

您需要使用CAST MULTISET来提取所需的数据;查询的结构应如下所示:

select multiple_tables(
       cast (multiset (select 1 from dual connect by level < 10 ) as number_table ),
       cast (multiset (select 1 from dual connect by level < 10 ) as number_table )
       )
from dual
使用和操作员:

WITH table_A ( value ) AS (
  SELECT LEVEL
  FROM   DUAL
  CONNECT BY LEVEL < 5
),
table_B ( id ) AS (
  SELECT 2*LEVEL
  FROM   DUAL
  CONNECT BY LEVEL < 4
)
SELECT  multiple_tables(
          CAST(
            MULTISET (
              SELECT * FROM table_a
            )
            AS number_table
          ),
          CAST(
            MULTISET (
              SELECT * FROM table_b
            )
            AS number_table
          )
        ) AS multi_tables
FROM    DUAL;
MULTI_TABLES
----------------------------------------------------------
MULTIPLE_TABLES(NUMBER_TABLE(1,2,3,4),NUMBER_TABLE(2,4,6))