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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:26:30+00:00 2026-05-14T23:26:30+00:00

Using MS SQL 2005, Table 1 ID | T1Value | T2ID | GroupID ———————————-

  • 0

Using MS SQL 2005,

Table 1

ID |  T1Value  | T2ID  |  GroupID
----------------------------------
1  |     a     |  10   |     1
2  |     b     |  11   |     1
3  |     c     |  12   |     1
4  |     a     |  22   |     2

Table 2

ID  |  T2Value
----------------
10  |     H
11  |     J
12  |     K
22  |     H

I want to clone the data for GroupID == 1 into a new GroupID so that I result with the following:

Table 1

ID |  T1Value  | T2ID  |  GroupID
----------------------------------
1  |     a     |  10   |     1
2  |     b     |  11   |     1
3  |     c     |  12   |     1
4  |     a     |  22   |     2
5  |     a     |  23   |     3
6  |     b     |  24   |     3
7  |     c     |  25   |     3

Table 2

ID  |  T2Value
----------------
10  |     H
11  |     J
12  |     K
22  |     H
23  |     H
24  |     J
25  |     K

I’ve found some SQL clone patterns that allow me to clone data in the same table well… but as I start to deal with cloning data in two tables at the same time and then linking up the new rows correctly… that’s just not something I feel like I have a good grasp of.

I thought I could do some self-joins to deal with this, but I am worried in the cases where the non-key fields have the same data in multiple rows.

  • 1 1 Answer
  • 2 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-14T23:26:30+00:00Added an answer on May 14, 2026 at 11:26 pm

    Is my only option to use a cursor to keep track of the ID mapping? Here is some pseudo-code I wrote… not tested yet. I was hoping for something more concise. Is this my only option?

    DECLARE @NewT2Key INT
    DECLARE @OldT2Key INT
    DECLARE @T2Value  VARCHAR(50)
    DECLARE @T2KeyNewOld TABLE (OldT2Key INT, NewT2Key INT)
    
    DECLARE @NewGroupID INT
    DECLARE @OldGroupID INT
    
    SET @NewGroupID = 3
    SET @OldGroupID = 1
    
    --
    -- STEP 1: CLONE THE TABLE2 DATA AND KEEP MAPPING OF OLD-to-NEW IDs
    --
    DECLARE curT2Keys
    CURSOR FAST_FORWARD LOCAL FOR
        SELECT t2.ID,
            t2.T2Value
        FROM dbo.Table2 t2
            JOIN dbo.Table1 t1
            ON  t2.ID    = t1.T2ID
        WHERE t1.GroupID = @OldGroupID
        ORDER BY t1.ID
    
    OPEN curT2Keys
    FETCH NEXT FROM curT2Keys INTO @OldT2Key, @T2Value
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @NewT2Key = (SELECT MAX(ID)+1 FROM dbo.Table2)
    
        INSERT INTO dbo.Table2(ID, T2Value)
        VALUES(@NewT2Key, @T2Value)
    
        INSERT INTO @T2KeyNewOld(OldT2Key, NewT2Key)
        VALUES(@OldT2Key, @NewT2Key)
    
        FETCH NEXT FROM curT2Keys INTO @OldT2Key, @T2Value
    END
    CLOSE curT2Keys
    DEALLOCATE curT2Keys
    
    --
    -- STEP 2: CLONE THE TABLE1 DATA AND UPDATE IDs WITH NEW MAPPING
    --
    
          INSERT INTO dbo.Table1([ID], [T1Value], [T2ID], [GroupID])
          (SELECT
                (SELECT MAX(ID) FROM dbo.Table1) + ROW_NUMBER() OVER (ORDER BY GroupID),
                t1.[T1Value],
                t2.[NewT2Key],
                @NewGroupID
           FROM dbo.Table1 t1 
               JOIN @T2KeyNewOld t2
               ON t1.T2ID = t2.OldT2Key
           WHERE t1.GroupID = @OldGroupID
          )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I want to export SQL Server 2005 table into XML using programatically. there
[using SQL Server 2005] I have a table full of users, I want to
I am trying to insert certain values into the table using SQL server 2005.
I'm using Sql-Server 2005 I have Users table with userID and gender. I want
Using SQL Server 2005 I want to generate the table values between the two
Using SQL Server 2005 I want to get only numeric values from the table
I'm using sql server 2005/2008 and I have a table that it's PK (rec_index)
I am using sql server 2005 and want to insert a row into a
I'm using SQL Server 2005 with a table with a primary key field with
Using SQL Server 2005 and 2008. I've got a potentially very large table (potentially

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.