I want to create a new table in SQL Server with the following query. I am unable to understand why this query doesn't work.
Query1: Works
SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2
Query2: Does not Work. Error: Msg 170, Level 15, State 1, Line 7 Line 7: Incorrect syntax near ')'.
SELECT * INTO [NEW_TABLE] FROM ( SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2 )
Thanks!
The UNION operator is used to combine the data from the result of two or more SELECT command queries into a single distinct result set. This operator removes any duplicates present in the results being combined.
The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types.
You have to define a table alias for a derived table in SQL Server:
SELECT x.* INTO [NEW_TABLE] FROM (SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2) x
"x" is the table alias in this example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With