[英]CTE and table update in ORACLE
在通过更新表中的列最终存储结果之前,我想运行很多复杂的逻辑。 我收到一个错误,并且能够将其归结为:
with my_cte as
(
select x,ix from y
)
update z
set mycol = (select x from my_cte where z.ix = my_cte.ix)
然而,这给出了错误:
Error at line 4:
ORA-00928: missing SELECT keyword
set mycol = (select x from my_cte where z.ix = my_cte.ix)
这是否仅仅意味着 CTE 不能与更新一起使用,因为以下查询工作正常:
update z
set mycol = (select x from y where y.ix = my_cte.ix)
使用版本 12c Enterprise Edition Release 12.1.0.2.0
编辑:
在解决这个问题一段时间后,获得合理性能的唯一方法是使用 MERGE 子句(仍然使用 CTE,如下面的答案)。
merge into z using (
with my_cte as (
select x,ix from y
)
)
on (
my_cte.ix = z.ix
)
when matched then
update set mycol = my_cte.x
在 Oracle 中,CTE 是SELECT
而不是UPDATE
:
update z
set mycol = (
with my_cte as (
select x, ix
from y
)
select x from my_cte where z.ix = my_cte.ix
);
如果 z.ix - 是主 kay 并且 y.ix - 是 z.ix 的外键,你可以写
update (select y.x, z.mycol
from y, z
where y.ix = x.ix)
set mycol = x;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.