2013-11-22 84 views
0

我有一個的SQL語句是這樣的:甲骨文11,更新加入

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 Error:
ORA-01779: cannot modify a column which maps to a non key-preserved table 01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.

+2

也許提供表的例子,說明你有什麼要做。 –

+0

你的dbms抱怨,因爲它不認爲它保證只在你的選擇結果中看到每個tbl記錄。所以你不能更新這個視圖,而是必須更新表。 –

回答

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; 
+0

但是在TABLE1上,我可以有許多相同的記錄(VAL_CODE,TV_CODE)。我必須添加「FLD_ID」才能獲取唯一身份記錄。 – user1093588

+0

你嘗試過使用索引而不是唯一索引嗎? – SriniV

+0

和TABLE2,我必須使用VAL_CODE,OMA_CODE,MEN_CODE,GOV_CODE – user1093588