Link two temp tables to get a result set

Need to make a temp table look at the results from another temp table and only bring back results based on the list of people's ID's in the first temp table.  I have both queries built and bringing back results.  

Need to know how to link the two temp tables and bring back a result set based on both sets.

Example:
IF OBJECT_ID('tempdb..#TEMP1') IS NOT NULL
      DROP TABLE #TEMP1
GO
SELECT pat_mrn, pat_name, disch_date
INTO #TEMP1
FROM ...
WHERE...
-----then
IF OBJECT_ID('tempdb..#TEMP2') IS NOT NULL
DROP TABLE #TEMP2
GO
SELECT pat.MRN, lab.ID
INTO #TEMP2
FROM...
WHERE...
---now what?
I need the pat_mrn, pat_name, disch_date, lab_id but ONLY if the pat_mrn is in both sets of data.
Becky EdwardsEpic Clarity DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
Select
t1.pat_mrn
, t1.pat_name
, t1.disch_date
, t2.lab_id
From #TEMP1 t1
inner join #TEMP2 t2
on t1.pat_mrn = t2.[pat.MRN]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
i think it should be:

Select t1.pat_mrn , t1.pat_name , t1.disch_date , t2.lab_id From #TEMP1 t1 inner join #TEMP2 t2 on t1.pat_mrn = t2.MRN

instead?
0
Mike EghtebasDatabase and Application DeveloperCommented:
I gave

on t1.pat_mrn = t2.[pat.MRN]

for Becky to link the tables in whatever form is possible. It was not clear to me what the joining columns exactly are.
0
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
I only tried the first one and it worked perfectly.  Thank you both for your comments!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.