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

后端 未结 6 1006
遥遥无期
遥遥无期 2020-11-27 08:28

The Query below is suited in SQL sErver. But in DB2 it does not give results:

Error is  SQLCODE = -199, ERROR:  ILLEGAL USE OF KEYWORD FROM. 
6条回答
  •  不知归路
    2020-11-27 08:50

    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.

提交回复
热议问题