我如何更新from语句中的字段?

时间:2015-11-30 16:15:34

标签: sql oracle sql-update

UPDATE
    RU
SET
    RU.Carrier = E1.TCarrier,
    RU.Carrier_Tracking_Number = E4.Bill_Ladin
FROM Rec_Unit RU
INNER JOIN Exp02 E2 ON (E2.Coment1 = RU.Pallet_ID)
INNER JOIN Exp05 E5 ON (E5.No_Packing = E2.No_Packing)
INNER JOIN Exp04 E4 ON (E4.No_Factura = E5.No_Factura)
INNER JOIN Exp01 E1 ON (E1.No_Packing = E2.No_Packing)
WHERE RU.Event = 'RESULT'
AND RU.Carrier IS NULL
AND RU.Carrier_Tracking_Number IS NULL
AND E1.TCarrier IS NOT NULL
AND E4.Bill_ladin IS NOT NULL

我正在尝试使用其他表中的值更新表Rec_Unit,但该语句不起作用,我使用此帖子中的语法How do I UPDATE from a SELECT in SQL Server?

这是SQL错误:

SQL Error: ORA-00933: SQL command not properly ended

1 个答案:

答案 0 :(得分:3)

我会改用合并语句。可能如下所示:

merge into rec_unit tgt
using (select ru.primary_key_col, -- amend the name of the primary key column(s) as appropriate
              e1.tcarrier,
              e4.bill_ladin
       FROM   Rec_Unit RU
              INNER JOIN Exp02 E2 ON (E2.Coment1 = RU.Pallet_ID)
              INNER JOIN Exp05 E5 ON (E5.No_Packing = E2.No_Packing)
              INNER JOIN Exp04 E4 ON (E4.No_Factura = E5.No_Factura)
              INNER JOIN Exp01 E1 ON (E1.No_Packing = E2.No_Packing)
       WHERE  RU.Event = 'RESULT'
       AND    RU.Carrier IS NULL
       AND    RU.Carrier_Tracking_Number IS NULL
       AND    E1.TCarrier IS NOT NULL
       AND    E4.Bill_ladin IS NOT NULL) src
  on (tgt.primary_key_col = src.primary_key_col) -- amend the name of the primary key column(s) as appropriate
when matched then
update set tgt.carrier = src.tcarrier,
           tgt.carrier_tracking_number = src.bill_ladin;