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

The Archive Base Latest Questions

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

I the following two tables/columns: TableA id | data TableB fkeyA1 | fkeyA2 |

  • 0

I the following two tables/columns:

TableA
id | data

TableB
fkeyA1 | fkeyA2 | fkeyA3 | name

The first three columns in TableB are foreign keys all pointing to TableA. I would like to transform TableB into the following two tables:

TableC
id | name

TableD
fkeyC | fkeyA

so that one row in TableB becomes one row in TableC plus three rows in TableD. How should I write a SQL query for this?

EDIT:

In TableB, there is a unique index on (fkeyA1, fkeyA2, fkeyA3), but the name column does not necessarily have unique values.

  • 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-26T23:46:28+00:00Added an answer on May 26, 2026 at 11:46 pm

    You can use a cursor statement:

    declare @idC table(id int)
    declare @fkeyC int, @fkeyA1 int, @fkeyA2 int, @fkeyA3 int, @name varchar(8)
    declare CursorB cursor fast_forward for select fkeyA1, fkeyA2, fkeyA3, name from TableB
    open CursorB
    fetch next from CursorB into @fkeyA1, @fkeyA2, @fkeyA3, @name
    while @@fetch_status = 0
    begin
        insert into TableC(name) output inserted.id into @idC values (@name)
        set @fkeyC = (select top 1 id from @idC)
        insert into TableD(fkeyC, fkeyA) values (@fkeyC, @fkeyA1), (@fkeyC, @fkeyA2), (@fkeyC, @fkeyA3)
        delete from @idC
        fetch next from CursorB into @fkeyA1, @fkeyA2, @fkeyA3, @name
    end
    close CursorB
    deallocate CursorB
    

    This may not be the best in performance, but will allow for non-unique values for the name column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say for example, I have the following two tables: TableA { _id } TableB
I have two tables with the following columns: table1: id, agent_name, ticket_id, category, date_logged
I've got the following two tables (in MySQL): Phone_book +----+------+--------------+ | id | name
I have the following two tables: Customer { int Id int Name } Bills
I have the following two tables: Table1 ---------- ID Name 1 A 2 B
I have two tables, both with the same columns. The first table contains original
I have two tables in my database. The first one, [nodeActivity] has the following
My data model contains two tables with composite primary keys and an associative table.
I have two tables with the following columns: Table1 { ID NUMBER(15), ROLL_NUM VARCHAR2(9),
i have two data tables with the same structure the first one has one

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.