在嵌套表中插入多个引用

时间:2017-03-21 17:31:11

标签: sql oracle reference nested-table object-oriented-database

我的表customer_table包含对account_table行的引用列表(嵌套表)。

以下是我的声明:

客户类型:

CREATE TYPE customer as object(
    custid integer,
    infos ref type_person,
    accounts accounts_list
);

accounts_list类型:

CREATE TYPE accounts_list AS table of ref account;

表:

CREATE TABLE customer_table OF customer(
    custid primary key,
    constraint c_inf check(infos is not null),
    constraint c_acc check(accounts is not null)
)
NESTED TABLE accounts STORE AS accounts_refs_nt_table;

所以我想在创建客户时在我的嵌套表中插入多个引用,因为可以共享一个帐户。

我无法知道如何做到这一点。

我试过了:

INSERT INTO customer_table(
    SELECT 0,
    ref(p),
    accounts_list(
        SELECT ref(a) FROM account_table a WHERE a.accid = 0
        UNION ALL 
        SELECT ref(a) FROM account_table a WHERE a.accid = 1
    )
    FROM DUAL
    FROM person_table p
    WHERE p.personid = 0
);

没有成功。

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用the collect() function,例如在子查询中:

INSERT INTO customer_table(
    SELECT 0,
    ref(p),
    (
      SELECT CAST(COLLECT(ref(a)) AS accounts_list) 
      FROM account_table a
      WHERE accid IN (0, 1)
    )
    FROM person_table p
    WHERE p.personid = 0
);

正如文档所说,“要从此函数中获得准确的结果,您必须在CAST函数中使用它”,因此我明确地将其强制转换为account_list类型。

如果您不想要子查询,则可以改为:

INSERT INTO customer_table(
    SELECT 0,
    ref(p),
    CAST(COLLECT(a.r) AS accounts_list)
    FROM person_table p
    CROSS JOIN (SELECT ref(a) AS r FROM account_table a WHERE accid IN (0, 1)) a
    WHERE p.personid = 0
    GROUP BY ref(p)
);

但我认为这有点麻烦;检查两者的表现虽然......