我正在使用以下查询(对于一个列,即base_price工作正常)但是当我尝试更新多个列时,我得到ORA-01767:UPDATE ... SET表达式必须是子查询。
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;
经过一番调查后,我找到了一个解决方案,可以通过再次从with子句中选择来完成,但我无法在查询reference中实现它。
任何人都知道怎么做?
答案 0 :(得分:1)
您无法从同一个子查询更新多个列。您可以将子查询重用于其他列(我不建议这样做,因为查询将执行两次),或者您可以使用oracle' s 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 = ..