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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:22:50+00:00 2026-06-15T00:22:50+00:00

Hope some one can help me, I would like to copy rows in same

  • 0

Hope some one can help me, I would like to copy rows in same table and that table has relation to another table that I have to copy related row accordingly:

Table1

       table1Id table0Id otherColumn 
          1         3        8
          2         3        9
          3         4        6

I copied rows with table0Id = 3

Table1

       table1Id table0Id otherColumn 
          1         3        8
          2         3        9
          3         4        6
        -------------------------
          4         3        8
          5         3        9

I would like to do the same to Table2 depending to the Table1 Ids like this:

Table2

       table2Id table1Id otherColomn
           1        1        0
           2        2        5
           3        3        8

Table2

       table2Id table1Id otherColomn
           1        1        0
           2        2        5
           3        3        8
         -----------------------
           4        4 new Id 0
           5        5 new Id 5

As you see row 1 and 2 are copied in table2 but they have new Ids from newly added rows in table1.
I know how to do the firs part but I’m stuck at the second part.

  • 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-15T00:22:51+00:00Added an answer on June 15, 2026 at 12:22 am

    Thanks for your answers, I figured it out like this by using output and identity:

    Declare @temp table(tempId int,tempName nchar(10),tempOther bit, rownumber int identity)
    insert into @temp select Id,Nam,other from dbo.Table_1  where Nam = 'ToCopy'
    
    Declare @tempIds table(outputId int,rownumber int identity)
    
    Declare @finalTemp table(OldId int,new_id int)
    
    select * from dbo.Table_1
    select * from dbo.Table_2
    
    insert into dbo.Table_1
    output inserted.Id into @tempIds
    select tempName,tempOther from @temp
    
    
    insert into @finalTemp
    select oldT.tempId, newT.outputId 
    from @temp as oldT
    join @tempIds as  newT on oldT.rownumber = newT.rownumber
    
    insert into dbo.Table_2(Table1Id)
    select ftemp.new_id
    from dbo.Table_2 t2 
    join @finalTemp ftemp on ftemp.OldId = t2.Table1Id
    
    
    select * from dbo.Table_1
    select * from dbo.Table_2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck in a rut. I hope some one can help. Basically, I am
I am having a JQuery/ASP issue that I hope someone can help me with.
I've been searching everywhere for this, hope some of you experts can help me
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
Hope someone can help i cant seem to get my head around this, i
Hope someone can help me out with this problem I am having. I just
I hope someone can help me here. I'm having trouble loading this variable. Right
I hope someone can help me with this, as I'm unable to find the
I hope someone can help here, we're having an incredibly annoying time with Visual
I hope someone can help, I am trying to enable enable ASP.Net Impersonation under

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.