Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am trying to update a table which is missing some data, but having problems with the update statement.

I have the following tables and data:

Customer table 1:

CustomerID: 1234
CustomerName: John
CustomerContact: 0189541145

Orders table 2:
CustomerId: blank
OrderNumber: 12345
OrderType: paper

how can i update the table to also have customer ID:

SQL
update Orders
set customerID = c.CustomerID
from Customer
where customerID = 12345
Posted
Comments
[no name] 29-Jun-15 7:37am    
--UPDATE
UPDATE ORDERS SET CUSTOMERID =(SELECT C.CUSTOMERID FROM CUSTOMER C WHERE C.CUSTOMERID = 12345)

OR

--DECLARE
DECLARE @CUSTOMERID INT
--SET
SET @CUSTOMERID = CUSTOMERID FROM CUSTOMER WHERE CUSTOMERID = 12345
--UPDATE
UPDATE ORDERS SET CUSTOMERID = @CUSTOMERID

1 solution

Since there is nothing common between the customer and orders table and the customer id is blank, there is no way to set null customer ids to a value automatically.

You have to manually update rows in the Order table by using the order id -
Update Orders set customerId = ?? where Order Number = 123456
 
Share this answer
 
Comments
Geofferz 29-Jun-15 7:21am    
Awesome thanks that helped me point me in right direction :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900