Search code examples
sqloracle-databaseoracle11guser-defined-types

How do I create an Oracle table of objects containing nested tables?


My question is similar to 9454933 but my nested table is within the object.

I wish to create a table of user defined types where the defined type contains an object, my simplified setup looks like this

CREATE TYPE DEMO_TYPE1 AS OBJECT (A1 NUMBER, A2 NUMBER);

CREATE TYPE DEMO_TAB_TYPE1 AS TABLE OF DEMO_TYPE1;

CREATE TYPE DEMO_TYPE2 AS OBJECT (B1 NUMBER, B2 DEMO_TAB_TYPE1);

CREATE TABLE DEMO_TAB1 (C1 NUMBER, C2 DEMO_TYPE2);

If I run the above I get the following error

The storage clause is not specified for the nested table column or attribute.

This makes sense but I can't work out the correct syntax to fix this I've tried

CREATE TABLE DEMO_TAB1 (C1 NUMBER, C2 DEMO_TYPE2)
NESTED TABLE B2 STORE AS B2_TAB;

but this and every other variation I've tried result in ORA-00922: missing or invalid option

So how do I resolve this?


Solution

  • The reason of the error is that the C2 is not a nested table. The nested table is the B2 inside C2, so you should try this:

    CREATE TABLE DEMO_TAB1 (C1 NUMBER, C2 DEMO_TYPE2)
    NESTED TABLE C2.B2 STORE AS B2_TAB;
    

    See sqlfiddle: sqlfiddle.com/#!4/6c662/1