merge two tables into one
I would like join two tables into 1 table that ha开发者_Python百科s one common column and I need to try and avoid all duplicates.
look at this (new in SQL Server 2008): MERGE (Transact-SQL)
Performs insert, update, or delete operations on a target table based on the results of a join with a source table. For example, you can synchronize two tables by inserting, updating, or deleting rows in one table based on differences found in the other table.
Perhaps you want a SELECT DISTINCT
and a LEFT JOIN
You can do a UNION and insert into the new table (note that you have to create the table first)
INSERT INTO NewTable
SELECT Column1
FROM FistTable
UNION
SELECT Column2
FROM SecondTable
Hope this will help
MERGE <target_table> [AS TARGET]
USING <table_source> [AS SOURCE]
ON <search_condition>
[WHEN MATCHED
THEN <merge_matched> ]
[WHEN NOT MATCHED [BY TARGET]
THEN <merge_not_matched> ]
[WHEN NOT MATCHED BY SOURCE
THEN <merge_ matched> ];
精彩评论