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 8640303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:14:47+00:00 2026-06-12T11:14:47+00:00

I have an adjacent list hierarchy model that makes up a topic structure ID

  • 0

I have an adjacent list hierarchy model that makes up a topic structure

   ID   Parent_Id Topic_Name
   1    Null      Topic 1
   2    Null      Topic 2
   3    2            Topic 3
   4    3               Topic 4
   5    2            Topic 5
   6    Null      Topic 6

This forms part of an application which I cant change – the topics dont have multiple parents so unfortunatly I can’t move to a nested sets – although if this was an interim step in the process – this would be fine as long as it went back to adjacent list hierarchy model

I want to specify a topic id and then copy it to a new topic id and retain the levels / structure underneath

So in my example I could specify topic topic_id 2 and it would create

   ID   Parent_Id Topic_Name
   7    Null      Topic 2
   8    7            Topic 3
   9    8               Topic 4
   10   7            Topic 5

Auto numbering is taken care of for the ID so no need to construct that, but obviously the parent id needs to be retained

How can I achieve the above? would I need to flatten the data and do 3 seperate inserts logging the id after each insert?

  • 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-06-12T11:14:49+00:00Added an answer on June 12, 2026 at 11:14 am

    You can use a recursive CTE to get the rows to insert.
    If you use merge to add the rows you can use output to capture a mapping between the generated ID and the old ID which can be used to update the column Parent_ID for the inserted rows.

    -- ID for topic to copy 
    declare @ID int;
    set @ID = 2;
    
    -- Table to hold the inserted rows
    declare @T table
    (
      New_ID int,
      Old_ID int,
      Old_ParentID int
    );
    
    -- Add rows from recursive CTE using merge
    with C as
    (
      select T.ID, T.Parent_Id, T.Topic_Name
      from YourTable as T
      where T.ID = @ID
      union all
      select T.ID, T.Parent_Id, T.Topic_Name
      from YourTable as T
        inner join C 
          on C.ID = T.Parent_Id
    )
    merge YourTable
    using C
    on 0 = 1
    when not matched then
      insert (Topic_Name) values (C.Topic_Name)
    output inserted.ID,
           C.ID,
           C.Parent_Id
      into @T(New_ID, Old_ID, Old_ParentID);
    
    -- Update Parent_Id for the new rows
    update Y set
      Parent_Id = T2.New_ID
    from @T as T1
      inner join @T as T2
        on T1.Old_ParentID = T2.Old_ID
      inner join YourTable as Y
        on T1.New_ID = Y.ID;
    

    SE-Data

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a list that looks like this: <ul> <li id=q></li> <li
For example I have long list of buttons: <input type=button name=clickbutton onclick='dosomething(this)'> But instead
I currently have a CSV that I have built using StringBuilder in C#. This
I have a loop of this structure Reference : Maxwell Code Example do z=1,zend
I have a list of (label, count) tuples like this: [('grape', 100), ('grape', 3),
I have a function (mergeall) that returns a float list. I want to calculate
I have a MySQL table called category that holds hierarchical data as an adjacent
I have an HTML table with collapsed and adjacent borders and a standard border
What I have to do here is to count the number of adjacent white
have written this little class, which generates a UUID every time an object of

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.