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

后端 未结 6 928
遥遥无期
遥遥无期 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:43

    In case someone else want to update a table with the SUM() of values of another Table on DB2. I was running into many issues with joins and finally a simple SELECT in the UPDATE solved it. Thx to @mustaccio

    TABLE_A
    ID......|TOTAL...|...
    1
    5
    6
    
    TABLE_B
    ID......|REF_ID...|QUANTITY|...
    1        5         20
    2        1         25
    3        1         3
    
    SQL Statement:  
    UPDATE TABLE_A ta
    SET ta.TOTAL = (
        SELECT SUM(tb.QUANTITY)
        FROM TABLE_B tb
        WHERE tb.REF_ID = ta.ID
    )
    
    RESULT
    TABLE_A
    ID......|TOTAL...|...
    1        28
    5        20
    6        NULL
    
    0 讨论(0)
  • 2020-11-27 08:48

    If you want to use UPDATE rather than MERGE, you want to process only the matching records where the value will change.

    UPDATE Sales_Import SI
      SET SI.AccountNumber = 
            (SELECT RAN.AccountNumber
               FROM RetrieveAccountNumber RAN
               WHERE SI.LeadID = RAN.LeadID
               FETCH FIRST ROW ONLY
            )
      WHERE SI.LeadID IN
            (SELECT S2.LeadID
               FROM Sales_Import S2
               JOIN RetrieveAccountNumber R2
                 ON S2.LeadID = R2.LeadID
               WHERE S2.AccountNumber <> R2.RetrieveAccountNumber 
            )
    
    0 讨论(0)
  • 2020-11-27 08:49

    response to your question

    UPDATE Sales_Import f1
    SET f1.AccountNumber = 
    (
     SELECT f2.AccountNumber
     FROM RetrieveAccountNumber f2
     WHERE f1.LeadID = f2.LeadID
     FETCH FIRST ROW ONLY
    )
    WHERE exists
    (
    SELECT * FROM RetrieveAccountNumber f2
     WHERE f1.LeadID = f2.LeadID 
    )
    

    template methode

    update table1 f1
    set (f1.field1, f1.field2, f1.field3, f1.field4)=
    (
    select f2.field1, f2.field2, f2.field3, 'CONSTVALUE'
    from table2 f2
    where (f1.key1, f1.key2)=(f2.key1, f2.key2) 
    )
    where exists 
    (
    select * from table2 f2
    where (f1.key1, f1.key2)=(f2.key1, f2.key2)
    )   
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 08:58

    I'm pretty sure (although I've not used DB2 in a while) that DB2 still does not support joins in update statements, so you'll need to use MERGE;

    Something like this (freehanding it since I don't have DB2 available, so may be slightly off);

    MERGE INTO Sales_Import si
    USING (SELECT AccountNumber, LeadID FROM RetrieveAccountNumber) ra
    ON (si.LeadID = ra.LeadID)
    WHEN MATCHED THEN
     UPDATE SET AccountNumber = ra.AccountNumber
    
    0 讨论(0)
  • 2020-11-27 09:04
    UPDATE USERS.A A SET A.NAME=(SELECT B.NAME FROM USERS.B B  WHERE A.ID=B.ID );
    
    0 讨论(0)
提交回复
热议问题