为什么我不能使用 With 关键字同时更新多个列?

Why can#39;t I update more than one column at the same time using With keyword?(为什么我不能使用 With 关键字同时更新多个列?)
本文介绍了为什么我不能使用 With 关键字同时更新多个列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的更新语句,它工作正常,我在子查询中使用了一个 with 语句来大大提高性能,但由于某种原因,我不允许从同一个表中添加一个额外的列来更新.

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.

作品:

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);

我想要做的只是包括另一个列,也像这样从 table_two 更新

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);

但我得到 ora-01767 更新集表达式必须是子查询.我理解这个错误,但看不到我是如何生成它的.非常感谢任何帮助.

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.

提前致谢.

推荐答案

这似乎可行(无论如何,它通过使用 DUAL 进行简单查询):

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)
                                        );

至于子查询以WITH"开头为什么不起作用,我只能想象是因为Oracle SQL的设计者没有预料到这种用法.

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.

这篇关于为什么我不能使用 With 关键字同时更新多个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)