sql server update one table from another code example

Example 1: sql update from another table

UPDATE table1 
   SET table1.Price = table2.price 
   FROM table1 INNER JOIN table2 ON table1.ID = table2.id
   -- WHERE table1.id=...

Example 2: sql update from different table

# SQL Server
UPDATE
    Sales_Import
SET
    Sales_Import.AccountNumber = RAN.AccountNumber
FROM
    Sales_Import SI
INNER JOIN
    RetrieveAccountNumber RAN
ON 
    SI.LeadID = RAN.LeadID;
# MySQL & MariaDB
UPDATE
    Sales_Import SI,
    RetrieveAccountNumber RAN
SET
    SI.AccountNumber = RAN.AccountNumber
WHERE
    SI.LeadID = RAN.LeadID;

Example 3: sql server update column based on another table

UPDATE
    Sales_Import
SET
    Sales_Import.AccountNumber = RAN.AccountNumber
FROM
    Sales_Import SI
INNER JOIN
    RetrieveAccountNumber RAN
ON 
    SI.LeadID = RAN.LeadID;

Tags:

Sql Example