2017-03-21 2 views
0

Ich habe die Tabelle customer_table mit einer Liste (geschachtelte Tabelle) von Referenzen auf Zeilen der account_table.Mehrere Referenzen in eine geschachtelte Tabelle einfügen

Hier sind meine Erklärungen:

Kundentyp:

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

accounts_list Typ:

CREATE TYPE accounts_list AS table of ref account; 

Tabelle:

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; 

So würde ich mehrere Refs einfügen möchte im Meine geschachtelte Tabelle beim Erstellen eines Kunden, da ein Konto freigegeben werden kann.

Ich kann nicht herausfinden, wie das geht.

Ich habe versucht:

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 
); 

ohne Erfolg.

Danke

Antwort

1

Sie the collect() function verwenden können, beispielsweise in einer Unterabfrage:

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 
); 

Da die Dokumentation sagt: „Um genaue Ergebnisse aus dieser Funktion erhalten Sie es innerhalb einer CAST Funktion verwenden müssen“, so habe ich warf es explizit auf Ihre account_list Typ.

Wenn Sie nicht über eine Unterabfrage wollen Sie stattdessen tun könnte:

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) 
); 

aber ich denke, das ist ein bisschen unordentlicher; Überprüfen Sie die Leistung beider ...

Verwandte Themen