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.
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 keyDescription 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 Binsert into tableAselect b.*from tableB bwhere 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 aset description = b.descriptionfrom tableB bjoin tableA a on a.code = b.code and b.description <> a.description |
|
|
niechen861102
Starting Member
9 Posts |
Posted - 2010-06-20 : 22:59:10
|
spam removed |
|
|
|
|
|