oracle 11,更新加入

时间:2013-11-22 10:54:22

标签: sql oracle join

我有一个像这样的SQL语句:

UPDATE tbl
SET old_ht = new_ht,
    old_ttc = new_ttc
from table1 tbl 
join table2 temp ON trim(temp.val_code) = trim(tbl.val_code) 
                 AND trim(temp.tv_code) = trim(tbl.tv_code)
INNER JOIN table3 tbl3 ON  trim(tbl3.oma_CODE) =trim(temp.oma_CODE) 
                       AND trim(tbl3.men_CODE) =trim(temp.men_CODE) 
                       AND  trim(tbl3.gov_CODE) =trim(gov.BRD_CODE)
                       and tbl3.fld_id = tbl.fld_id  ;

但似乎Oracle不支持这种语法。

我也试过这个:

UPDATE ( select tbl.cost_ht as old_ht, temp.cost_ht as new_ht,tbl.cost_ttc as old_ttc
                ,  temp.cost_ttc as new_ttc
         from table1 tbl 
         join table2 temp ON trim(temp.val_code) = trim(tbl.val_code) 
                 AND trim(temp.tv_code) = trim(tbl.tv_code)
         INNER JOIN table3 tbl3 ON  trim(tbl3.oma_CODE) =trim(temp.oma_CODE) 
                       AND trim(tbl3.men_CODE) =trim(temp.men_CODE) 
                       AND  trim(tbl3.gov_CODE) =trim(gov.BRD_CODE)
                       and tbl3.fld_id = tbl.fld_id )
SET old_ht = new_ht, old_ttc = new_ttc

但是我收到了这个错误:

错误报告:

  

SQL错误:
  ORA-01779:无法修改映射到非密钥保留表01779的列.00000 - “无法修改映射到非密钥保留表的列”
  *原因:尝试插入或更新连接视图的列   映射到非密钥保留的表。
  *操作:直接修改基础表。

1 个答案:

答案 0 :(得分:0)

就这么简单..通过这个索引为您的引擎提供更多信心。

CREATE UNIQUE INDEX IDX1
    ON TABLE1 ( VAL_CODE,TV_CODE );

CREATE UNIQUE INDEX IDX2
    ON TABLE2 ( VAL_CODE,TV_CODE, OMA_CODE ,MEN_CODE );

CREATE UNIQUE INDEX IDX3
    ON TABLE3 ( OMA_CODE ,MEN_CODE, GOV_CODE  );

然后尝试你的查询,这应该运行良好

UPDATE
      (SELECT
            TBL.COST_HT AS OLD_HT,
            TEMP.COST_HT AS NEW_HT,
            TBL.COST_TTC AS OLD_TTC,
            TEMP.COST_TTC AS NEW_TTC
       FROM
            TABLE1 TBL
            JOIN TABLE2 TEMP
                ON TRIM ( TEMP.VAL_CODE ) = TRIM ( TBL.VAL_CODE )
                   AND TRIM ( TEMP.TV_CODE ) = TRIM ( TBL.TV_CODE )
            INNER JOIN TABLE3 TBL3
                ON      TRIM ( TBL3.OMA_CODE ) = TRIM ( TEMP.OMA_CODE )
                   AND TRIM ( TBL3.MEN_CODE ) = TRIM ( TEMP.MEN_CODE )
                   AND TRIM ( TBL3.GOV_CODE ) = TRIM ( GOV.BRD_CODE )
                   AND TBL3.FLD_ID = TBL.FLD_ID)
SET
      OLD_HT        = NEW_HT,
      OLD_TTC     = NEW_TTC;