SQL update from one Table to another based on a ID match IN db2

DB2 does indeed support joins in an UPDATE statement, only not the way you think — DB2 follows the SQL ANSI standard:

UPDATE
     Sales_Import SI
 SET
    Sales_Import.AccountNumber = (
      SELECT 
        RAN.AccountNumber
      FROM
        RetrieveAccountNumber RAN
      WHERE  
        SI.LeadID = RAN.LeadID
    )

The above assumes that LeadID uniquely identifies records in RetrieveAccountNumber, otherwise you will get an error because the subquery would return more than one row.

Edit:

To address comments below, if no matching record in RetrieveAccountNumber can be found, Sales_Import.AccountNumber will be set to null. If this is undesirable, one could use COALESCE() to assign a default value.

Leave a Comment