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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:59:46+00:00 2026-05-28T17:59:46+00:00

I have two table. OLDTABLE and NEWTABLE. I need to insert some data from

  • 0

I have two table. OLDTABLE and NEWTABLE.
I need to insert some data from oldtable to newtable and the get the ID from new table and update in the oldtable for that corresponding row. the newtable ID has seperate column in oldtable which is null by default.

One way is to get the ID from both table while inserting the data in new table and then later find the data in oldtable and update it. Which is simpler way.

Can anyone suggest some optimized way to do so?

  • 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-28T17:59:48+00:00Added an answer on May 28, 2026 at 5:59 pm

    Basically, there is no magic here – you need to insert the rows into newtable and keep track of the id’s – both the “old” ones from the oldtable as well as the new ones in new table.

    So basically, you need something like this:

    -- declare a helper table variable
    DECLARE @TableOfID TABLE (OldID INT, NewID INT)  
    
    -- insert rows into the "newtable" - OUTPUT the inserted ID (assuming it's INT IDENTITY)
    -- and the "old ID" into the table variable
    INSERT INTO dbo.newTable(OldID, Col1, Col2, ..., ColN)
    OUTPUT Inserted.ID, Inserted.OldID INTO @TableOfID(NewID, OldID)
       SELECT 
           ID, Col1, Col2, .., ColN
       FROM 
           dbo.oldtable
       WHERE 
          ..... (some WHERE conditions here)
    
    -- update the "oldtable"
    UPDATE 
        dbo.oldtable
    SET 
        NewID = t.NewID
    FROM 
        @TableOfID t
    WHERE
        dbo.oldtable.ID = t.OldID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

High level I have two tables that need to have some of the data
I have two table and I need to copy the data across from SRCServiceUsers
I have two table views displaying the data from a Sqlite database. I can
I have two table view filled in their viewDidLoad with data coming from a
I have a table with data.i'm going to transfer data between two tables(using Insert
I have some data that isn't properly partitioned (for lack of a better word).
I have two table in database A,B A: summary view of my data B:
i'm trying to migrate some data from two tables in an OLD database, to
I have two table where two column are fixed. Some columns are identical and
I have two databases: old and new. Both have a comments table. There are

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.