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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:55:56+00:00 2026-05-20T22:55:56+00:00

I’m trying to query 2 tables to generate records for a third table which

  • 0

I’m trying to query 2 tables to generate records for a third table which only sometimes links to both of the other tables, and insert the ID generated for the third table into a fourth table that I can use to prevent re-generation if already generated records in the third table. Below is a simplified example of what I’m trying to do.

create table #T1(id1 int primary key identity(100,1) not null, value nvarchar(10))
create table #T2(id2 int primary key identity(200,1) not null, value nvarchar(10))
insert into #T1(value) values('a')
insert into #T1(value) values('b')
insert into #T1(value) values('c')
insert into #T2(value) values('c')
insert into #T2(value) values('b')
insert into #T2(value) values('d')
create table #T3(id3 int primary key identity(300,1) not null, id2 int null, value nvarchar(10))
create table #T3Info(id1 int not null, id3 int not null)

insert into #T3(id2, value)
output inserted.id2
,#T1.id1
into #T3Info(id1, id3)
select #T2.id2, #T1.value
from #T1
left join #T2 on #T1.value = #T2.value
left join #T3Info join #T3 on #T3.id3 = #T3Info.id3
 on #T3Info.id1 = #T1.id1
where #T3Info.id1 is null

I can’t do this because #T1.id1 is not being inserted into #T3. Without altering the schema of #T1 through #T3, what can I do to get the information I want into #T3Info?

I would like to end up with:

T3:

    id3  |  id2   |  value
---------+--------+---------
    300  |  NULL  |  a
    301  |   201  |  b
    302  |   200  |  c

T3Info:

   id1  |  id3
--------+--------
   100  |  300
   101  |  301
   102  |  302
  • 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-20T22:55:56+00:00Added an answer on May 20, 2026 at 10:55 pm

    I think this statement will do the trick!

    merge into #T3
    using(
    select #T1.id1, #T1.value
    ,#T2.id2
    ,#T3Info.id3
    from #T1
    left join #T2 on #T1.value = #T2.value
    left join #T3Info on #T3Info.id1 = #T1.id1) src
    on src.id3 = #T3.id3
    when not matched by target then
    insert(id2, value) values(src.id2, src.value)
    output src.id1, inserted.id3
    into #T3Info;
    

    Any comments about the reliability of this?

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

Sidebar

Related Questions

No related questions found

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.