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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:30:31+00:00 2026-06-01T04:30:31+00:00

I am a beginner at SQL and I don’t know much about Transact-SQL. I

  • 0

I am a beginner at SQL and I don’t know much about Transact-SQL.

I realize this is a newbie question, but I’m looking for a simple solution.

I have a table with some columns (locationCode, CustomerCode).

Primary table

    ItemCode    locationCode    CustomerCode
    I001        001001          C001 
    I002        001002          C001    
    I003        001001          C002
    I004        002001          C002

I want to select data from this table and insert to two others table.

First table

firstTblId(autoIncrement)  warehouseCode  CustomerCode
       1                       001           C001   
       2                       001           C002
       3                       002           C002

warehouseCode is a combination of first three characters from locationCode

Data in first table are grouped by first three char of locationCode and Customer Code

second table

secondTblId
(autoIncrement)  ItemCode  locationCode  CustomerCode   firstTblId(FK)
      1            I001       001001        C001            1
      2            I002       001002        C001            1
      3            I003       001001        C002            2
      4            I004       002001        C002            3

So, how can I insert first table and second table by selecting primary table’s rows with SQL??

Thanks you for all of your replies.

  • 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-06-01T04:30:32+00:00Added an answer on June 1, 2026 at 4:30 am

    I think you want something like the below. The temporary table @Output will capture the inserted identities for the first table, then these can be used when inserting to the second table.

    DECLARE @Output TABLE 
    (       FirstTableID    INT NOT NULL PRIMARY KEY, 
            WarehouseCode   VARCHAR(3), 
            CustomerCode    VARCHAR(4)
    )
    INSERT INTO FirstTable (WarehouseCode, CustomerCode)
    OUTPUT inserted.FirstTblID, inserted.WarehouseCode, inserted.CustomerCode INTO @Output
    SELECT  DISTINCT LEFT(LocationCode, 3) [WarehouseCode], CustomerCode
    FROM    [PrimaryTable]
    
    INSERT INTO SecondTable (ItemCode, LocationCode, CustomerCode, FirstTblID)
    SELECT  p.ItemCode,
            p.LocationCode,
            p.CustomerCode, 
            o.FirstTableID
    FROM    [PrimaryTable] p
            INNER JOIN @Output o
                ON LEFT(LocationCode, 3) = WarehouseCode
                AND p.CustomerCode = o.CustomerCode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry that I put this question because I think that is simple but
This is a beginner question, I have to create some SQL Reports with business
This is a beginner question, but it's been frustrating me... I am using C#,
im pretty much a beginner to using ASP.net and linq to sql but what
SQL Server Beginner question: I'm trying to introduce a computed column in SQL Server
Please help this beginner here... I have a SQL Server 2008 R2 running on
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Absolute beginner question: I have a template file index.html that looks like this: ...
I'm Java beginner, but I thought that when using try-catch-finally I don't have to
I'm pretty experienced in C#, but still mostly a beginner in SQL. We 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.