2016-10-31 177 views
4

在我最终通过更新表中的列来存储结果之前,我想要运行很多复杂的逻辑。我得到一个错误,并且已经能够得到它归结为:ORACLE中的CTE和表更新

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) 

这是否仅仅意味着热膨胀系数不能与更新,因为下面的查询作品使用精细:

update z 
set mycol = (select x from y where y.ix = my_cte.ix) 

使用12C版企业版发布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 
+0

CTE仍然可以用作更新子查询,如下所示:https://stackoverflow.com/a/39534514/603516 – Vadzim

回答

6

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

如果z.ix - 是主要凯y.ix - 为外键z.ix你可能会写

update (select y.x, z.mycol 
      from y, z 
      where y.ix = x.ix) 
    set mycol = x;