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

The Archive Base Latest Questions

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

I would like to be able to clone a record and its descendants in

  • 0

I would like to be able to clone a record and its descendants in the same table. An example of my table would be the following:

Table1

id | parentid | name
---------------------
 1 |    0     |  'Food'
 2 |    1     |  'Taste'
 3 |    1     |  'Price'
 4 |    2     |  'Taste Requirements'

The “id” column is the primary key and auto-increments. The ‘Food’ record (i.e. where id = 1) has two records underneath it called ‘Taste’ and ‘Price’. The ‘Taste’ record has a record underneath it called ‘Taste Requirements’. I would like to be able to clone the ‘Food’ record so that Table1 would look like the following:

Table1

id | parentid | name
---------------------
 1 |    0     |  'Food'
 2 |    1     |  'Taste'
 3 |    1     |  'Price'
 4 |    2     |  'Taste Requirements'
 5 |    0     |  'Cookies'
 6 |    5     |  'Taste'
 7 |    5     |  'Price'
 8 |    6     |  'Taste Requirements'

(where ‘Cookies’ is the name of the new category that I want to create). I am able to select all the descendants of ‘Food’ using:

with Table1_CTE( id, parentid, name )
as
(
  select t.id, t.parentid, t.name from Table1 t
    where t.id = 1
  union all
  select t.id, t.parentid,t. name from Table1 t
    inner join Table1_CTE as tc
      on t.parentid = tc.id
)
select id, parentid, name from Table1_CTE

and I am able to clone just the ‘Food’ record (i.e. where id = 1) using:

insert into Table1 ( parentid, name )
  select ( parentid, 'Cookies' ) 
  from Table1 where id = 1

but I am having problems trying to combine the two queries to clone the descendants of ‘Food’. Also, I am trying to avoid using stored procedures, triggers, curosrs, etc. Is what I am trying to do possible? I have seen some examples on the web but have been unable to apply them to my requirements.

  • 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-21T22:43:37+00:00Added an answer on May 21, 2026 at 10:43 pm

    As Martin suggested, you need to enable IDENTITY_INSERT so that you can push your own identity values. You may also need to acquire a table lock to ensure that Max( Id ) returns the correct value.

    If object_id('tempdb..#TestData') is not null
        Drop Table #TestData
    GO
    Create Table #TestData
        (
        Id int not null identity(1,1) Primary Key
        , ParentId int not null
        , Name varchar(50) not null
        )
    GO
    Set Identity_Insert #TestData On
    GO  
    Insert #TestData( Id, ParentId, Name )
    Values( 1,0,'Food' )
        , ( 2,1,'Taste' )
        , ( 3,1,'Price' )
        , ( 4,2,'Taste Requirement' );
    
    
    With Data As
        (
        Select Cast(MaxId.Id + 1 As int) As Id
            , T.ParentId
            , 'Copy Of ' + T.name As Name
            , T.Id As OldId
            , 0 As OldParentId
        From #TestData As T
            Cross Join( Select Max( id ) As Id From #TestData ) As MaxId
        Where T.Name = 'Food'
        Union All
        Select Cast(Parent.id + Row_Number() Over( Order By Child.Id ) + 1 As int)
            , Parent.Id
            , 'Copy of ' + Child.Name
            , Child.Id
            , Child.ParentId
        From Data As Parent
            Join #TestData As Child
                On Child.ParentId = Parent.OldId
        )
    Insert #TestData( Id, ParentId, Name )
    Select Id, ParentId, Name
    From Data
    GO
    Set Identity_Insert #TestData Off
    GO  
    

    Results

    id | parentid | name
    -- | -------- | -----------------
    1  | 0        | Food
    2  | 1        | Taste
    3  | 1        | Price
    4  | 2        | Taste Requirement
    5  | 0        | Copy Of Food
    7  | 5        | Copy of Taste
    8  | 5        | Copy of Price
    9  | 7        | Copy of Taste Requirement
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to add a new SQL LOGIN and name
I would like to be able to use the same Session variable when transferring
Would like to be able to set colors of headings and such, different font
I would like to be able to change the size of text in a
I would like to be able to abort a specific sql statement if it
I would like to be able to get a list of all possible files
I would like to be able to automate data entry to an Open Office
I would like to be able to spawn a linux process that would only
I would like to be able to massage certain categories of news feeds to
I would like to be able to calculate the family relationship between two individuals

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.