Share via

2 query results into temp table

Raj0125 511 Reputation points
2022-06-17T13:51:56.38+00:00

Hi,

I want to achive these result set into Temp table.Pleas suggest.

SELECT ID, Reference
FROM Emp
WHERE ID NOT IN (SELECT tID FROM Dept)
UNION
SELECT O.ID, ''
FROM Emp P, Dept O
WHERE P.tID = O.ID
ORDER BY 1

Azure SQL Database
0 comments No comments

Answer accepted by question author

Alberto Morillo 35,506 Reputation points MVP Volunteer Moderator
2022-06-17T14:29:06.66+00:00

Try the following:

SELECT t.* INTO #TempTable1  
FROM  
(  
SELECT ID, Reference  
FROM Emp  
WHERE ID NOT IN (SELECT tID FROM Dept)  
UNION  
SELECT O.ID, ''  
FROM Emp P, Dept O  
WHERE P.tID = O.ID  
ORDER BY 1  
) t  
  
SELECT * FROM #TempTable1  
  
DROP TABLE IF EXISTS #TempTable1  

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.