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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:07:49+00:00 2026-05-11T22:07:49+00:00

If I were to have 2 tables, call them TableA and TableB. TableB contains

  • 0

If I were to have 2 tables, call them TableA and TableB. TableB contains a foreign key which refers to TableA. I now need to add data to both TableA and TableB for a given scenario. To do this I first have to insert data in TableA then find and retrieve TableA’s last inserted primary key and use it as the foreign key value in TableB. I then insert values in TableB. This seems lika a bit to much of work just to insert 1 set of data. How else can I achieve this? If possible please provide me with SQL statements for SQL Server 2005.

  • 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-11T22:07:49+00:00Added an answer on May 11, 2026 at 10:07 pm

    That sounds about right. Note that you can use SCOPE_IDENTITY() on a per-row basis, or you can do set-based operations if you use the INSERT/OUTPUT syntax, and then join the the set of output from the first insert – for example, here we only have 1 INSERT (each) into the “real” tables:

    /*DROP TABLE STAGE_A
    DROP TABLE STAGE_B
    DROP TABLE B
    DROP TABLE A*/
    SET NOCOUNT ON
    
    CREATE TABLE STAGE_A (
        CustomerKey varchar(10),
        Name varchar(100))
    CREATE TABLE STAGE_B (
        CustomerKey varchar(10),
        OrderNumber varchar(100))
    
    CREATE TABLE A (
        Id int NOT NULL IDENTITY(51,1) PRIMARY KEY,
        CustomerKey varchar(10),
        Name varchar(100))
    CREATE TABLE B (
        Id int NOT NULL IDENTITY(1123,1) PRIMARY KEY,
        CustomerId int,
        OrderNumber varchar(100))
    
    ALTER TABLE B ADD FOREIGN KEY (CustomerId) REFERENCES A(Id);
    
    INSERT STAGE_A VALUES ('foo', 'Foo Corp')
    INSERT STAGE_A VALUES ('bar', 'Bar Industries')
    INSERT STAGE_B VALUES ('foo', '12345')
    INSERT STAGE_B VALUES ('foo', '23456')
    INSERT STAGE_B VALUES ('bar', '34567')
    
    DECLARE @CustMap TABLE (CustomerKey varchar(10), Id int NOT NULL)
    
    INSERT A (CustomerKey, Name)
    OUTPUT INSERTED.CustomerKey,INSERTED.Id INTO @CustMap
    SELECT CustomerKey, Name
    FROM STAGE_A
    
    INSERT B (CustomerId, OrderNumber)
    SELECT map.Id, b.OrderNumber
    FROM STAGE_B b
    INNER JOIN @CustMap map ON map.CustomerKey = b.CustomerKey
    
    SELECT * FROM A
    SELECT * FROM B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables; let's call them TableA and TableB. Each element of TableB
I have 2 tables, call A and B. A has a foreign key on
I have an array which contains 2 types of items (lets call them type
I have three tables. Let's call them a, b, and a_to_b. a_to_b contains (among
I have an Access Form whose source data is 2 tables, let's call them
I have two tables that I want to fetch data from. Lets call them
I have two tables, let's just call them A & B, which can be
I have a database with two tables - let's call them Foo and Bar.
I have two tables linked by an ID column. Let's call them Table A
I have two tables, let's call them table1 and table2 . They both have

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.