-
How can I update the values of one table with that of another?
I have a situation where we have shipping names in our orders table and I would like to place a customer's last name into the field where a null resides.
The orders table is joined to the customers table by a customer id and the field in the customers table that I would like to use in the shipping name in the orders table is the last name.
Please help!
-
use tempdb
create table orders (orderid int, shippingname varchar(100), customerid int)
create table customer(customerid int, fname varchar(50), lname varchar(50))
insert into orders select 1,null,1
insert into orders select 2,'MAK',1
insert into orders select 3,'Claire',2
insert into orders select 4,null,1
insert into orders select 5,null,3
insert into orders select 6,'xyz',4
insert into orders select 7,'ss',2
insert into customer select 1,'x','MAK'
insert into customer select 2,'y','Claire'
insert into customer select 3,'xs','Smith'
insert into customer select 4,'ex','temp'
insert into customer select 5,'we','Scott'
select * from orders
update a set a.shippingname =b.lname from orders a,customer b where
a.customerid=b.customerid and a.shippingname is null
select * from orders
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|