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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:35:05+00:00 2026-06-04T13:35:05+00:00

I have a temporary table derived from a number of tables, which needs to

  • 0

I have a temporary table derived from a number of tables, which needs to be inserted into two tables (T1 and T2). T1 has a primary key (auto generated), which must be inserted into T2 as a foreign key (One : Many relationship)

I know if I used the following insert statement

INSERT INTO T1 (.....)
SELECT (.....) FROM X

I am unable to use the scope_identity, as that would only give the me last auto generated ID, to use in T2.

Other than either using a curser or looping though each row, what options exist, for ensuring the relationships between records split across tables remain? Just for information, this insert process happens regularly, and could included up 1000+ records across the two tables.

  • 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-04T13:35:07+00:00Added an answer on June 4, 2026 at 1:35 pm

    The “Output clause” can solve your problem I think. An example

    create table itest ( i int identity not null primary key, j int not null unique )
    create table #new ( i int not null, j int not null)
    insert into itest (j)
    output inserted.i, inserted.j into #new
    select o.object_id from sys.objects as o
    select * from #new
    drop table #new, itest;
    go
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Stored procedure which return some fields from a temporary table
I have a temporary table (or, say, a function which returns a table of
I have problems filling a temporary table with the resultset from a procedure call
I have a database that has two tables. These two tables are defined as:
I have an Oracle global temporary table which is ON COMMIT DELETE ROWS. I
I have a stored procedure inside which I create a temporary table that typically
I have created a temporary table using cakephp $this->Model->query() and inserted some records using
I have read that creating a temporary table is best if the number of
I have a table containing a large number of rows. Each row has 2
Ok I have a temporary MySQL table with 135,000 rows, from this temporary table

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.