How to move UNION query results to a new table?
insert into temp_UNION
select * from (
SELECT *
FROM [#temp1]
UNION
SELECT *
FROM [#temp2]
UNION
SELECT *
FROM [#temp3]
UNION
SELECT *
FROM [#temp4]
UNION
SELECT *
FROM [#temp5]
)
In SQL Server you have to use
SELECT <COLUMNS_LIST>
INTO <NEW_TABLE_NAME>
FROM <TABLES, WHERE ETC>
More information @ http://msdn.microsoft.com/en-us/library/ms188029.aspx
Try this:
SELECT *
INTO #temp_UNION
FROM
(
SELECT *
FROM [#temp1]
UNION
SELECT *
FROM [#temp2]
UNION
SELECT *
FROM [#temp3]
UNION
SELECT *
FROM [#temp4]
UNION
SELECT *
FROM [#temp5]
) a