Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4563230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:19:43+00:00 2026-05-21T18:19:43+00:00

I have following table structure ID Name Parent_ID 1 abc 0 2 efg 1

  • 0

I have following table structure

ID  Name  Parent_ID

1   abc   0

2   efg   1

3   hij   1

4   klm   2

5   nop   3

and so on….id is generated in a sequence

I want a PL/SQL to coy this same data in same table but id should be generated by seq and Parent_ID should be mapped accordingly…that means..after PL/SQL it should look like

ID  Name  Parent_ID

1   abc   0

2   efg   1

3   hij   1

4   klm   2

5   nop   3

6   abc   0

7   efg   6

8   hij   6

9   klm   7

10  nop   8

can any1 help me in this…thnx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:19:43+00:00Added an answer on May 21, 2026 at 6:19 pm

    So, here is your original data:

    SQL> select * from t23
      2  /
    
            ID NAM  PARENT_ID
    ---------- --- ----------
             1 abc          0
             2 efg          1
             3 hij          1
             4 klm          2
             5 nop          3
    
    SQL>
    

    This procedure populates a PL/SQL collection with the extant rows. It loops through those rows, populating an associative array with a new ID which is indexed by the original ID. (Note the assignment uses the 11g syntax for getting a sequence value, rather than the traditional selecting from DUAL). The ID is then chnaged to the new value, and the PARENT_ID is updated with the value stored in the associative array. Lastly the new rows are inserted into the table using the bulk FORALL syntax,

    SQL> declare
      2      type num_lookup is table of pls_integer
      3          index by pls_integer;
      4      id_translate num_lookup;
      5
      6      type t23_nt is table of t23%rowtype;
      7      new_rows t23_nt;
      8  begin
      9      select *
     10      bulk collect into new_rows
     11      from t23
     12      order by id asc;
     13
     14      for i in new_rows.first()..new_rows.last()
     15      loop
     16          id_translate(new_rows(i).id) := s23.nextval;
     17          new_rows(i).id := s23.currval;
     18          if new_rows(i).parent_id != 0
     19          then
     20              new_rows(i).parent_id := id_translate(new_rows(i).parent_id);
     21          end if;
     22      end loop;
     23
     24      forall j in new_rows.first()..new_rows.last()
     25          insert into t23 values new_rows(j);
     26
     27  end;
     28  /
    
    PL/SQL procedure successfully completed.
    
    SQL> 
    

    And, lo!

    SQL> select * from t23;
    
            ID NAM  PARENT_ID
    ---------- --- ----------
             1 abc          0
             2 efg          1
             3 hij          1
             4 klm          2
             5 nop          3
             6 abc          0
             7 efg          6
             8 hij          6
             9 klm          7
            10 nop          8
    
    10 rows selected.
    
    SQL>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table reply of which structure is following: Id name parent_id ...
I have a table emp with following structure and data: name dept salary -----
I have the following table structure. I want to select distinct CustomerId and CustomerName
I have a basic recursive table setup in SQL 2008 with the following structure:
i have table with following structure, name (type: TEXT) price (type: INT) color (type:
I have a organization name table with the following structure given below: CREATE TABLE
I have a table structure like the following: table a: id int name varchar
I have created a table in MSSQL having following structure - **Table Name -
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
I have the following table structure and I want a turn the query into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.