Why can't I update more than one column at the same time using With keyword?

大城市里の小女人 提交于 2019-12-08 18:58:11

问题


I have an update statement shown below that works fine, I used a with statement in the subquery to greatly improve performance but for some reason I'm not allowed to add an additional column from the same table to update.

Works:

UPDATE Table_A SET (Col_One) = (WITH OneValue AS (SELECT DISTINCT t.Col_One
                                                  FROM Table_Two t, Table_A a
                                                  WHERE t.id = a.New_Id))
                                SELECT Col_One FROM OneValue);

What I'd like to do is just include another column to update also from table_two like this

UPDATE Table_A SET (Col_One, Col_Two) = (WITH OneValue AS (SELECT DISTINCT t.Col_One, T.Col_two
                                                  FROM Table_Two t, Table_A a
                                                  WHERE t.id = a.New_Id))
                                SELECT Col_One, Col_Two FROM OneValue);

but I get ora-01767 update set expression must be a subquery. I understand this error but fail to see how I'm generating it. Any help is greatly appreciated.

Thanks in advance.


回答1:


This appears to work (it did with a simple query using DUAL anyway):

UPDATE Table_A SET (Col_One, Col_Two) = (select col_one, col_two from
                                          (WITH OneValue AS (SELECT DISTINCT t.Col_One, T.Col_two
                                                  FROM Table_Two t, Table_A a
                                                  WHERE t.id = a.New_Id))
                                           SELECT Col_One, Col_Two FROM OneValue)
                                        );

As for why it doesn't work if the subquery starts with "WITH", I can only imagine that it is because the designers of Oracle SQL hadn't anticipated this usage.



来源:https://stackoverflow.com/questions/10917381/why-cant-i-update-more-than-one-column-at-the-same-time-using-with-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!