Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Prashanth.

Asked: October 12, 2016 - 11:53 am UTC

Last updated: November 11, 2016 - 4:07 pm UTC

Version: Oracle 12c

Viewed 1000+ times

You Asked


I would like to know if there is any easier way to replicate a nested table.

Say for a normal table, we can just do CREATE TABLE a_bkp as SELECT * FROM a;

Is there a similar easy way to create replica of existing nested table ?

and Connor said...

I'm not sure what you mean ... a simple assignment does the trick.

SQL> create or replace
  2  type ntt as table of number
  3  /

Type created.

SQL>
SQL> create or replace
  2  type ntt as table of number
  3  /

Type created.

SQL>
SQL> set serverout on
SQL> declare
  2    n ntt := ntt();
  3    clone ntt := ntt();
  4
  5  begin
  6    select object_id
  7    bulk collect into n
  8    from all_objects;
  9
 10    dbms_output.put_line(n.count);
 11
 12    clone := n;
 13
 14    dbms_output.put_line(clone.count);
 15  end;
 16  /
96989
96989

PL/SQL procedure successfully completed.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

nested Tables

Rajeshwaran, Jeyabal, October 13, 2016 - 6:35 am UTC

I think, they are looking for something like this.

Nested Table as a storage mechanism, not in plsql.

demo@ORA12C> create or replace type myobj
  2  is object(x int,y varchar2(30));
  3  /

Type created.

demo@ORA12C>
demo@ORA12C> create or replace type mytab
  2  is table of myobj;
  3  /

Type created.

demo@ORA12C> create table t( owner varchar2(30),
  2             object_type varchar2(30),
  3             object_details mytab )
  4  nested table object_details store
  5  as object_details_ntt;

Table created.

demo@ORA12C> insert into t(owner,object_type,object_details)
  2  select owner,object_type, cast( collect(myobj(object_id,object_name)) as mytab)
  3  from all_objects
  4  group by owner,object_type;

159 rows created.

demo@ORA12C> commit;

Commit complete.

demo@ORA12C> create table t2
  2  nested table object_details
  3  store as object_details_ntt2
  4  as
  5  select * from t;

Table created.

demo@ORA12C> set linesize 71
demo@ORA12C> desc t2
 Name                                Null?    Type
 ----------------------------------- -------- -------------------------
 OWNER                                        VARCHAR2(30)
 OBJECT_TYPE                                  VARCHAR2(30)
 OBJECT_DETAILS                               MYTAB

demo@ORA12C> select count(*) from t2;

  COUNT(*)
----------
       159

1 row selected.

demo@ORA12C>

nested tables

Prashanth Patkar, November 10, 2016 - 12:32 pm UTC

Thanks very much . I have another followup question , have been trying all day for a solution without success, appreciate if i could get some help.

I have nested table like below which is already loaded.

create or replace TYPE access_t AS OBJECT (
   AccessID        VARCHAR2(50),
   Eligibility       char(1)
   );
/

create or replace TYPE Access_tab IS TABLE OF access_t;
/

create or replace TYPE add_t AS OBJECT (
   city    VARCHAR2(100),
   state   VARCHAR2(100),
   zip     VARCHAR2(10),
   APOINTSARRAY Access_tab )
;
/

create or replace TYPE add_tab IS TABLE OF add_t;
/

CREATE TABLE RQST_STATUS
   (    RQST_ID NUMBER, 
        ADDRESS add_tab
   ) 
 NESTED TABLE ADDRESS STORE AS RQST_STATUS_ADDRESS
 ( NESTED TABLE APOINTSARRAY STORE AS RQST_STATUS_AP)
;


Now without changing TYPE add_t (since its used in other tables which do not need this change) , i need to add 2 additional columns to ADDRESS.
I took backup of the table , altered the table to add another new field.

create or replace TYPE add_t_new AS OBJECT (
   city    VARCHAR2(100),
   state   VARCHAR2(100),
   zip     VARCHAR2(10),
   APOINTSARRAY Access_tab,
   Latitude      VARCHAR2(10),
   Longitude     VARCHAR2(10),
)
;
/

create or replace TYPE add_tab_new IS TABLE OF add_t_new;
/

CREATE TABLE RQST_STATUS
   (    RQST_ID NUMBER, 
        ADDRESS add_tab
   ) 
 NESTED TABLE ADDRESS STORE AS RQST_STATUS_ADDRESS
 ( NESTED TABLE APOINTSARRAY STORE AS RQST_STATUS_AP)
;

ALTER TABLE RQST_STATUS ADD 
(
    ADDRESS_NEW   add_tab_new
) 
NESTED TABLE ADDRESS_NEW STORE AS ADDRESS_NEW_V1
  (NESTED TABLE APOINTSARRAY STORE AS APOINTSARRAY_V1) ;

So now , i have old and new fields in the same table. 
How can i now insert data from old field to new field ? with null values for the 2 additional fields.

I intend to drop the old column and then rename the new column to old after.
Thanks.


Chris Saxon
November 11, 2016 - 4:07 pm UTC

very helpful

Doug, April 10, 2019 - 6:22 pm UTC

I had the same question as the OP. Jeyabal's response was succinct and complete.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library