Groups | Blog | Home
all groups > sql server programming > november 2005 >

sql server programming : Updating one table with data from another


Alejandro Mesa
11/7/2005 5:30:11 PM
Try,

update tableB
set
HasShipped = (select shipped from tableA as a where a.ordernum =
tableB.orderid),
ship_date = (select shipdate from tableA as a where a.ordernum =
tableB.orderid)
where
exists(select * from tableA as a where a.ordernum = tableB.orderid)

-- or

update b
set
b.HasShipped = a.shipped,
b.ship_date = a.shipdate
from
tableB as b
inner join
tableA as a
on b.orderid = a.ordernum
go


AMB

[quoted text, click to view]
stjulian
11/7/2005 8:09:00 PM
I have a need to update a table with data from another. They do not even
share the same field names. although do share the same data types and
meanings. I am just having a problem wrapping my head around this. I can't
seem to shake the feeling that I am going to update the entire table by
accident.

The idea is to update an order table (B) with data from another type of
order table (A). Sometimes TableA is blank, if that is important.

The order ID's are the common links and are unique.


for example:

TableA ---> TableB
---------- -----------
ordernum = orderid
shipped HasShipped
shipdate ship_date




Thanks
Julian


AddThis Social Bookmark Button