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

  • Home
  • SEARCH
  • 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 8768811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:09:44+00:00 2026-06-13T17:09:44+00:00

I have two tables, Table_1 and Table_2 . Table_1 has columns PK (autoincrementing int

  • 0

I have two tables, Table_1 and Table_2.

Table_1 has columns PK (autoincrementing int) and Value (nchar(10)).

Table_2 has FK (int), Key (nchar(10)) and Value (nchar(10)).

That is to say, Table_1 is a table of data and Table_2 is a key-value store where one row in Table_1 may correspond to 0, 1 or more keys and values in Table_2.

I’d like to write code that programmatically builds up a query that inserts one row into Table_1 and a variable number of rows into Table_2 using the primary key from Table_1.

I can do it easy with one row:

INSERT INTO Table_1 ([Value])
OUTPUT INSERTED.PK, 'Test1Key', 'Test1Val' INTO Table_2 (FK, [Key], [Value])
VALUES ('Test')

But SQL doesn’t seem to like the idea of having multiple rows. This fails:

INSERT INTO Table_1 ([Value])
OUTPUT INSERTED.PK, 'Test1Key', 'Test1Val' INTO Table_2 (FK, [Key], [Value])
OUTPUT INSERTED.PK, 'Test2Key', 'Test2Val' INTO Table_2 (FK, [Key], [Value])
OUTPUT INSERTED.PK, 'Test3Key', 'Test3Val' INTO Table_2 (FK, [Key], [Value])
VALUES ('Test')

Is there any way to make this work?

  • 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-13T17:09:45+00:00Added an answer on June 13, 2026 at 5:09 pm

    I had to put the code in answer, in comment it looks ugly…

    CREATE TABLE #Tmp(PK int, value nchar(10))
    
    INSERT INTO Table_1 ([Value])
    OUTPUT INSERTED.PK, inserted.[Value] INTO #Tmp
    SELECT 'Test'
    
    INSERT INTO Table_2 (FK, [Key], Value)
    SELECT PK, 'Test1Key', 'Test1Val' FROM #Tmp
    UNION ALL SELECT PK, 'Test2Key', 'Test2Val' FROM #Tmp
    UNION ALL SELECT PK, 'Test3Key', 'Test3Val' FROM #Tmp
    

    Btw, SQL Server won’t let you do it all in one query without some ugly hack…

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

Sidebar

Related Questions

Lets say I have two tables: table english which has two columns, id and
Let's say I have two tables: Table 1 has the columns NOTE_ID (a unique
I have two tables suppose table 1 has two columns with short names and
I have two tables in which the second table has a foreign key which
I have two tables. Table #1 'color' has columns [red], [blue], [green]; where each
My question is: I have two tables: table A has two columns (KeyA and
I have two SQL Server tables. One table has four nvarchar(50) nullable columns and
I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns.
I have two tables - table1 and table2 table1 has 14 columns and table2
I have two tables (TABLE1, TABLE2 - unique i know) that has a 1-to-many

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.