Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Join Tables

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-04 : 09:29:08
Hi there,

I have two different tables in MyDatabase.

Table_1 and Table_2, with the same kind of columns (length and info)

I want to keep the information of Table_1, and ad all the rows that Table_2 has different from Table_1. That way I will have a New_Table will all the info non-duplicated from both tables.

Any idea?

Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-04 : 09:34:48
select col1, col2, ... from Table_1
UNION
select col1, col2, ... from Table_2

will give all rows from both tables but without duplicates.


Too old to Rock'n'Roll too young to die.
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-04 : 09:43:31
And sorry for asking again,

But where do I copy the non-equal information into a table (INTO Final_Table):

select col1, col2, ... from Table_1
UNION
select col1, col2, ... from Table_2

Thanks again
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-04 : 10:00:25
Is the destination table an existing table?

INSERT Final_Table(col1, col2, ...)
select col1, col2, ... from Table_1
UNION
select col1, col2, ... from Table_2


Too old to Rock'n'Roll too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-04 : 10:04:56
If the destination isn't existing:

select * into destination_table
from
(
select col1, col2, ... from Table_1
UNION
select col1, col2, ... from Table_2
)dt



Too old to Rock'n'Roll too young to die.
Go to Top of Page
   

- Advertisement -