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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:32:25+00:00 2026-05-11T05:32:25+00:00

i’d appreciate some help putting together a sql script to copy data from one

  • 0

i’d appreciate some help putting together a sql script to copy data from one table to another. essentially what i need to do for each row in the source table is aggregate the column values and store them into a single column in the target table.

TableA: ID, ColumnA, ColumnB, ColumnC TableB: Identity, ColumnX 

so, ColumnX needs to be something like ‘ColumnA, ColumnB, ColumnC’.

in addition though, i need to keep track of each TableA.ID -> SCOPE_IDENTITY() mapping in order to update a third table.

thanks in advance!

EDIT: TableA.ID is not the same as TableB.Identity. TableB.Identity will return a new identity value on insert. so either i need to store the mapping in a temp table or update TableC with each insert into TableB.

  • 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. 2026-05-11T05:32:26+00:00Added an answer on May 11, 2026 at 5:32 am

    Here is a row-by-row processing example. This will provide you the results in a way where you can process each row at a time. Or you can use TableC at the end and do whatever processing you need to do.

    However, it would be a lot faster if you added an extra column to TableB (Called TableA_ID) and just INSERTED the result into it. You would have instant access to TableA.ID and TableB.Identity. But without knowing your exact situation, this may not be feasible. (But you could always add the column and then drop it afterwards!)

    USE tempdb GO  CREATE TABLE TableA (     ID int NOT NULL PRIMARY KEY,     ColumnA varchar(10) NOT NULL,     ColumnB varchar(10) NOT NULL,     ColumnC varchar(10) NOT NULL )  CREATE TABLE TableB (     [Identity]  int IDENTITY(1,1) NOT NULL PRIMARY KEY,     ColumnX varchar(30) NOT NULL )  CREATE TABLE TableC (     TableA_ID  int NOT NULL,     TableB_ID  int NOT NULL,     PRIMARY KEY (TableA_ID, TableB_ID) )  GO  INSERT INTO TableA VALUES (1, 'A', 'A', 'A') INSERT INTO TableA VALUES (2, 'A', 'A', 'B') INSERT INTO TableA VALUES (3, 'A', 'A', 'C') INSERT INTO TableA VALUES (11, 'A', 'B', 'A') INSERT INTO TableA VALUES (12, 'A', 'B', 'B') INSERT INTO TableA VALUES (13, 'A', 'B', 'C') INSERT INTO TableA VALUES (21, 'A', 'C', 'A') INSERT INTO TableA VALUES (22, 'A', 'C', 'B') INSERT INTO TableA VALUES (23, 'A', 'C', 'C') GO  -- Do row-by-row processing to get the desired results SET NOCOUNT ON DECLARE @TableA_ID int DECLARE @TableB_Identity int DECLARE @ColumnX varchar(100)  SET @TableA_ID = 0  WHILE 1=1 BEGIN    -- Get the next row to process   SELECT TOP 1      @TableA_ID=ID,     @ColumnX = ColumnA + ColumnB + ColumnC   FROM TableA   WHERE ID > @TableA_ID    -- Check if we are all done   IF @@ROWCOUNT = 0     BREAK    -- Insert row into TableB   INSERT INTO TableB (ColumnX)     SELECT @ColumnX    -- Get the identity of the new row   SET @TableB_Identity = SCOPE_IDENTITY()    -- At this point, you have @TableA_ID and @TableB_Identity.   -- Go to town with whatever extra processing you need to do   INSERT INTO TableC (TableA_ID, TableB_ID)     SELECT @TableA_ID, @TableB_Identity  END GO  SELECT * FROM TableC GO 

    SELECT * FROM TableA

    ID          ColumnA    ColumnB    ColumnC ----------- ---------- ---------- ---------- 1           A          A          A 2           A          A          B 3           A          A          C 11          A          B          A 12          A          B          B 13          A          B          C 21          A          C          A 22          A          C          B 23          A          C          C 

    SELECT * FROM TableB

    Identity    ColumnX ----------- ------------------------------ 1           AAA 2           AAB 3           AAC 4           ABA 5           ABB 6           ABC 7           ACA 8           ACB 9           ACC 

    SELECT * FROM TableC

    TableA_ID   TableB_ID ----------- ----------- 1           1 2           2 3           3 11          4 12          5 13          6 21          7 22          8 23          9 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The timeout alone is not a good idea. Use one… May 14, 2026 at 10:25 pm
  • Editorial Team
    Editorial Team added an answer No, for more info on someone who wanted to do… May 14, 2026 at 10:25 pm
  • Editorial Team
    Editorial Team added an answer The simple answer is that you can't - types need… May 14, 2026 at 10:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.