2012-02-06 58 views
1

我在表之間建立了連接。我只是想從一些數據中獲取這些值並將其放入其他列中。但它不起作用。 我該如何重寫查詢? 我想用swkf的值覆蓋wkfc表中的值。Oracle:多表更新=> ORA-01779:無法修改映射到非鍵保存表的列

我知道這是正確的我怎麼才能讓Oracle簡單地做到這一點?

UPDATE (
SELECT --FROM 
     swkf.swkf_stato_workflow_id "swkf_swkf_stato_workflow_id", 
     swkf.swkf_data_ini "swkf_swkf_data_ini", 
     swkf.swkf_versione "swkf_swkf_versione", 
     swkf.spwkf_stato_pubblico_id "swkf_spwkf_stato_pubblico_id", 
     swkf.spwkf_data_ini "swkf_spwkf_data_ini", 
     swkf.spwkf_versione "swkf_spwkf_versione", 
     --TO 
     wkfc.swkf_stato_workflow_id "wkfc_swkf_stato_workflow_id", 
     wkfc.swkf_data_ini "wkfc_swkf_data_ini", 
     wkfc.swkf_versione "wkfc_swkf_versione", 
     wkfc.spwkf_stato_pubblico_id "wkfc_spwkf_stato_pubblico_id", 
     wkfc.spwkf_data_ini "wkfc_spwkf_data_ini", 
     wkfc.spwkf_versione "wkfc_spwkf_versione" 
     -- 
    FROM wkfb_stati_workflow swkf, wkf_cronologia wkfc 
WHERE twkf_tipo_workflow_id = 
      (SELECT twkf_tipo_workflow_id 
      FROM wkf_istanze_workflow wkfi, RET_PUNTI_EROGAZIONE RPUN 
      WHERE  RPUN.PUN_PUNTO_EROGAZIONE_COD = '8001375567' --codice puntero 
        AND RPUN.PUN_PUNTO_EROGAZIONE_ID = wkfi.ogg_oggetto_id 
        AND wkfi.tog_tipo_oggetto_id = 'RET_PUN1' 
        AND wkfi.WKFI_FLAG_ANN = 'N') 
     AND swkf_descrizione = '(O)Occupato' 
     AND wkfc.wkfc_cronologia_id = 'ApAJ0qCudNphjLxj' 
) a1 
set 
"wkfc_swkf_stato_workflow_id" = "swkf_swkf_stato_workflow_id" , 
"wkfc_swkf_data_ini" =   "swkf_swkf_data_ini" , 
"wkfc_swkf_versione" =   "swkf_swkf_versione" , 
"wkfc_spwkf_stato_pubblico_id" = "swkf_spwkf_stato_pubblico_id" , 
"wkfc_spwkf_data_ini" =   "swkf_spwkf_data_ini" , 
"wkfc_spwkf_versione" =   "swkf_spwkf_versione" ; 

這是同樣做如下的,但他必須自行找到值。

UPDATE wkf_cronologia 
    SET swkf_stato_workflow_id = 'o3gE1tlSdcDIC6FF', 
     swkf_data_ini = TO_TIMESTAMP ('19-06-2010 18:28:10,556000000','DD-MM-RRRR HH24:MI:SS,FF'), 
     swkf_versione = 0, 
     SPWKF_STATO_PUBBLICO_ID = '*1UNICOO', 
     SPWKF_DATA_INI = TO_TIMESTAMP ('01-01-0001 00:00:00,000000000', 'DD-MM-RRRR HH24:MI:SS,FF'), 
     SPWKF_VERSIONE = 0 
WHERE wkfc_cronologia_id = 'ApAJ0qCudNphjLxj'; --id del record di cronologia da aggiornare (estratto nella prima query) 
+1

我沒有看到你的表間的任何聯接標準這個連接。表wkfb_stati_workflow,wkf_cronologia,wkf_istanze_workflow和RET_PUNTI_EROGAZIONE如何關聯? – 2012-02-06 18:09:04

+0

他們不是。我需要從swkf中獲取唯一值並使用它們來更新wkf_cronologia。最好的方法是什麼? – Revious 2012-02-07 11:36:21

回答

3

如果你想UPDATE (SELECT ... FROM A INNER JOIN B ON condition) SET A.X = ...然後連接條件限制必須基於B有些唯一性約束的所有列到一個值。

3

首先,我們無法使用單個查詢通過連接視圖更新兩個表,我們可以選擇DB Procedure在這種情況下。

ORA-01776: cannot modify more than one base table through a join view 
    01776. 00000 - "cannot modify more than one base table through a join view" 
    *Cause: Columns belonging to more than one underlying table were either 
     inserted into or updated. 

但是我們可以使用連接來比較基表和參考表,我們可以更新一個表。

UPDATE 
(
    SELECT t1.d1 x, t2.d1 y FROM t1, t2 WHERE t1.d2 = t2.d2 AND t2.d2 = 2 
) t3 

SET t3.x = 6; 
-- t3.y = 1; this is wrong updating multiple table 

現在執行此連接(t1.d2 = t2.d2)應發生在唯一列上。 這裏這兩個表的d2列應該是唯一的。

否則,結果在下面

Oracle: multiple table updates => ORA-01779: 
cannot modify a column which maps to a non key-preserved table 


下面的網址介紹瞭如何執行非唯一列

Oracle - update join - non key-preserved table