简体   繁体   中英

Oracle nested table append

I have an oracle nested table as follows:

create table test_tab
(
Col1        VARCHAR,
Col2        VARCHAR,
Col3        VARCHAR,
Col4       Coltype,
PK(col1,col2,col3)
 );

Coltype definition:

create type Coltype as varray(10) of Coltuple;

Coltuple definition:

create type remarktuple as object
(
ColX varchar,
Coly varchar,
Colz varchar
);

and i have inserted a single row

insert into test_tab values('A','B','C',Coltype(Coltuple('X','Y','Z'));

now, if i want to inserted Coltype(Coltupele('P','Q','R')) to the same A,B,C row, how can i do it??... When i use a separate insert like insert into test_tab values('A','B','C',Coltype(Coltuple('P','Q','R')); , it is assuming it as a second insert and throwing error because of PK voilation.

Hope i have explained my requirement clearly. Thank you in advance.

With the above definition, Col4 is not a nested table; it's a varray. For Col4 to be a nested table, first, Coltype should be defined as:

CREATE TYPE Coltype AS TABLE of Coltuple;

Then, test_tab should be defined as something like the below:

CREATE TABLE test_tab
(
Col1        VARCHAR2(30),
Col2        VARCHAR2(30),
Col3        VARCHAR2(30),
Col4        Coltype,
PRIMARY KEY (col1,col2,col3)
)
NESTED TABLE Col4 STORE AS ColtypeStoreTab;

Now, with (Col1,Col2,Col3) being defined as the primary key of the parent table, it is not possible to insert another row with the values of ('A','B','C') while such a row already exists in the table unless the primary key constraint of the parent table is relaxed. Much probably , you wanted to insert ('P','Q','R') into the nested table of the parent table's row where (Col1,Col2,Col3) was ('A','B','C') . If that's the case, here is how it could be achieved:

INSERT INTO TABLE(SELECT Col4 FROM test_tab 
                  WHERE Col1 = 'A' AND Col2 = 'B' AND  Col3 = 'C')
VALUES('P','Q','R');

Another way to do it is this one:

UPDATE test_tab 
SET Col4 = Col4 MULTISET UNION Coltype(Coltuple('P','Q','R'))
WHERE Col1 = 'A' AND Col2 = 'B' AND  Col3 = 'C';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM