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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 merge two tables again

Author  Topic 

jzurbo77
Starting Member

21 Posts

Posted - 2010-06-14 : 15:41:12
How can I add records from one table to another? Both tables have same structure (see below). I have two independent tasks:

1) Table B (secondary) has among its rows few that do not exist in Table A (primary). Add rows, missing in table A from table B.

2) Update rows in Table A with values in Table B when there is a difference.

Table structure:

Code varchar(40) not null - primary key
Description varchar(60)

Thank you



pduffin
Yak Posting Veteran

68 Posts

Posted - 2010-06-18 : 23:36:24
Please test this in a test enviorment fist but I think this will work for you!
-- adds records to table A which aren't in table B
insert into tableA
select b.*
from tableB b
where b.code not in (select code from tableA)

-- Updates the description of table A to that of table B if they're different.
update table a
set description = b.description
from tableB b
join tableA a on a.code = b.code and b.description <> a.description
Go to Top of Page

niechen861102
Starting Member

9 Posts

Posted - 2010-06-20 : 22:59:10
spam removed
Go to Top of Page
   

- Advertisement -