2017-10-17 29 views
1

我正在使用以下查詢(對於一列即base_price正常工作),但是當我嘗試更新多個列時,我正在獲取ORA-01767:UPDATE .. 。SET表達式必須是子查詢。無法使用'with'子句更新多個列

UPDATE quote_item qi 
SET (qi.base_price,qi.base_cost) 
    = 
    (WITH baseVersion AS 
    (SELECT qi1.quote_oid, qi1.item_num, qi1.sub_item_num as sub_item_num, q1.quote_num, qi1.base_price, qi1.base_cost 
    FROM quote q1, 
    quote_item qi1 
    WHERE q1.oid   = qi1.quote_oid 
    AND q1.quote_type_oid=4 
    AND q1.version_num = 0 
) 
SELECT bv.base_price,bv.base_cost 
FROM baseVersion bv, 
    quote revQuote 
WHERE revQuote.oid  = qi.quote_oid 
AND revQuote.quote_num = bv.quote_num 
AND qi.item_num   =bv.item_num 
AND qi.sub_item_num  =bv.sub_item_num 
AND revQuote.version_num >0 and revQuote.quote_type_oid=4 
) 
    where qi.quote_type_oid=4; 

後一些環顧四周,我發現,它可以從與子句再次選擇做一個解決方案,但我不能夠實現它在我的查詢reference

任何人都知道如何做到這一點?

回答

1

無法從同一子查詢中更新多個列。您可以重新使用其他列的子查詢(我不建議這樣做,因爲查詢將執行兩次),或者您可以使用oracle的MERGE。它會看起來像這樣(調整它):

MERGE INTO quote_item qi 
USING (Your subquery goes here) t 
ON(t.oid  = qi.quote_oid 
    AND qi.item_num   =t.item_num 
    AND qi.sub_item_num  =t.sub_item_num) 
WHEN MATCHED THEN UPDATE 
    SET qi.col1 = .., 
     qi.col2 = ..