I have two tables, how do I copy/replace the data from column "OrderBy" to
column "Salesperson"? I want to replace the data in column "Salesperson"
with the data in column "OrderBy".
table INVOICES1 table INVOICES2
column Salesperson column OrderBy
Thanks.
RussHi Russ,
In order to update the Salesperson column with the OrderBy column you need
to have a corresponding column in both tables to join the tables. In the
example below I assumed you have invoice_no column:
UPDATE INVOICES1
SET Salesperson =
(SELECT OrderBy
FROM INVOICES2 AS I2
WHERE I2.invoice_no = INVOICES1.invoice_no)
WHERE EXISTS
(SELECT *
FROM INVOICES2 AS I3
WHERE I3.invoice_no = INVOICES1.invoice_no)
Please note that this will generate error if you have duplicate invoice_no
values in the INVOICES2 table.
You can get around that by using the proprietary SQL Server syntax for
update (but the result of the update will not be deterministic):
UPDATE INVOICES1
SET Salesperson = OrderBy
FROM INVOICES1 AS I1
JOIN INVOICES2 AS I2
ON I2.invoice_no = I1.invoice_no
HTH,
Plamen Ratchev
http://www.SQLStudio.com
No comments:
Post a Comment