2013-04-17 36 views
1

我想參考其他表更新表的兩列。執行腳本時顯示錯誤。如何更新與其他表連接的表中的列?

錯誤:錯誤的命令開始第1行:

UPDATE wb_costing_work_items, 
     sa_sales_documents, 
     sa_sales_document_items 
    SET cwi_price_per_hour = sdi_price, 
     cwi_amount = sdi_price * cwi_hours 
WHERE cwi_lo_id = sad_lo_id 
    AND sdi_sad_id = sad_id 
    AND sdi_wit_id = cwi_wit_id 
    AND cwi_id = 1650833 

Error at Command Line:1 Column:28 Error report: SQL Error: ORA-00971: missing SET keyword 00971. 00000 - "missing SET keyword"

SQL語句

UPDATE wb_costing_work_items cwi, 
     sa_sales_documents sad, 
     sa_sales_document_items sdi 
    SET cwi.cwi_price_per_hour = sdi.sdi_price, 
     cwi.cwi_amount = sdi.sdi_price * cwi.cwi_hours 
WHERE cwi.cwi_lo_id = sad.sad_lo_id 
    AND sdi.sdi_sad_id = sad.sad_id 
    AND sdi.sdi_wit_id = cwi.cwi_wit_id 
    AND cwi.cwi_id = 1650855 
+0

您想要更新哪張表? –

+0

我想更新表wb_costing_work_items的兩列值應該來自其他表 –

回答

0

事情是這樣的,也許。

請注意,我不得不使用一些關於哪個列屬於哪個表的野蠻猜測,因爲您沒有包含關於如何定義表的任何信息。

所以最可能我沒有得到它的權利,你將需要調整查詢到您的實際表結構。

merge into wb_costing_work_items 
using 
(
    select cwi.pk_column, -- don't know what the PK of the wb_costing_work_items is due to lack of information 
      sdi.sdi_price, -- don't know if this column really belong to this table 
      sdi.sdi_price * cwi.cwi_hours as total-- again unsure about column names due to lack of information 
    FROM wb_costing_work_items cwi 
     JOIN sa_sales_documents sad ON sad.sad_lo_id = cwi.cwi_lo_id -- not sure about these, due to lack of information 
     JOIN sa_sales_document_items sdi 
     ON sdi.sad_id = sad.sdi_sad_id  -- not sure about these, due to lack of information 
     AND sdi.sdi_wit_id = cwi.cwi_wit_id -- not sure about these, due to lack of information 

) t ON (t.pk_column = w.pk_column) -- not sure about this, due to lack of information 
when matched then 
update 
    set cwi_price_per_hour = t.sdi_price, 
     cwi_amount = t.total; 
+0

好吧,讓我測試它。 –

+0

這裏別名「w」代表什麼? –

+0

謝謝!其作品! –

1

這應該肯定有效。

  UPDATE (SELECT cwi_price_per_hour, 
          sdi_price, 
          cwi_amount, 
          sdi_price, 
          cwi_hours 
         FROM wb_costing_work_items, 
          sa_sales_documents, 
          sa_sales_document_items 
        WHERE  cwi_lo_id = sad_lo_id 
          AND sdi_sad_id = sad_id 
          AND sdi_wit_id = cwi_wit_id 
          AND cwi_id = 1650833) 
       SET cwi_price_per_hour = sdi_price, cwi_amount = sdi_price * cwi_hours 

請使用別名表和前綴列,以便可以輕鬆讀取您的查詢。

+0

我嘗試執行它顯示的語句錯誤行:12列:19 錯誤報告: SQL錯誤:ORA-01779:無法修改映射到非密鑰保存表的列 01779. 00000 - 「無法修改一個映射到非鍵保存表的列「 *原因:試圖插入或更新連接視圖的列,其中 映射到非鍵保留表。 *操作:直接修改底層基表。 –