带子查询别名的Oracle更新

时间:2014-10-28 19:56:46

标签: oracle join sql-update

我有两张桌子:

**PRODUCTS**:  
*PART   ITEM*  
M1      A1  
M1      A2  
M1      A3  
M2      B1  
M2      B2  
M3      C1  
M3      C2  
...  

**PARTS**:
*PART   CODE*
M1      XYZ
M2      XYZ
M3      ABC
A1      XYZ
A2      MNO
A3      <null>
B1      <null>
B2      <null>
C1      <null>
...

基本上,我想通过
更新PARTS表中的空值    - 从PARTS.CODE中获取一个现有代码    - 其中PART.PART = PRODUCT.PART
   - 在PRODUCT.ITEM上匹配PARTS.PART

到目前为止我所拥有的是:

update PARTS t2 
set t2.CODE =
  (
    select tx.CODE, t1.ITEM
    from PARTS tx
    join PRODUCTS t1
      on  tx.PART = t1.PART
  ) a
where t2.PART = a.ITEM
  and t2.CODE is null

内部选择会调出我需要的ITEM和CODE - 至少看起来它会在下面的'where'中匹配。我得到的错误是:
  错误:ORA-00933:SQL命令未正确结束

非常感谢......

1 个答案:

答案 0 :(得分:0)

update PARTS t2 
set t2.CODE = (select max(tx.CODE) 
                 from PARTS tx join PRODUCTS t1 on tx.PART = t1.part
                where t2.part = t1.item
              )
where t2.CODE is null
  and exists(select 1 from PRODUCTS t1 where t1.item = t2.part and t2.part = t1.part);