Hello. I'm having some difficulty trying to output the results of two seperate queries into the same temporary table.
Does anyone know if it is possible to use the UNION operator to output the results into a temporary table?
Select * From TableA
UNION
Select * From TableB
<--output result to temporary table here-->
Alternatively, is it possible to output the results of two queries in to the same temporary table without the UNION clause?
The following statement fails on the second SELECT INTO due to the fact that #MyTempTable already exists.
Select * INTO #MyTempTable FROM TableA
Select * INTO #MyTempTable FROM TableB
Thanks in advance.I think if you specify the SELECT * INTO in the top half of your union it ought to do the trick.
Yes this works...
USE NORTHWIND
GO
SELECT * INTO EMPLOYEES2
FROM EMPLOYEES
UNION ALL
SELECT * FROM EMPLOYEES
Points deducted for style though, never use SELECT * in production. Need a column list, do this...
SELECT COLUMN_NAME + ', '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Employees'
ORDER BY ORDINAL_POSITION|||Thanks Thrasymachus - that's exactly what I was looking for.
...and I'm sure you'll be pleased to hear the SELECT * was only for the example's in this thread. :D
No comments:
Post a Comment