表格1:
id name desc
-----------------------
1 a abc
2 b def
3 c adf
Run Code Online (Sandbox Code Playgroud)
表2:
id name desc
-----------------------
1 x 123
2 y 345
Run Code Online (Sandbox Code Playgroud)
在oracle SQL中,如何运行可以使用表2更新表1 并使用相同的sql更新查询?所以我得到的最终结果是namedescid
表格1:
id name desc
-----------------------
1 x 123
2 y 345
3 c adf
Run Code Online (Sandbox Code Playgroud)
问题来自更新一个表与来自另一个表的数据,但专门针对oracle SQL.
在我最终通过更新表中的列来存储结果之前,我想要运行许多复杂的逻辑.我收到一个错误,并且能够将其归结为:
with my_cte as
(
select x,ix from y
)
update z
set mycol = (select x from my_cte where z.ix = my_cte.ix)
Run Code Online (Sandbox Code Playgroud)
然而,这给出了错误:
Error at line 4:
ORA-00928: missing SELECT keyword
set mycol = (select x from my_cte where z.ix = my_cte.ix)
Run Code Online (Sandbox Code Playgroud)
这是否意味着CTE不能与更新一起使用,因为以下查询工作正常:
update z
set mycol = (select x from y where y.ix = my_cte.ix)
Run Code Online (Sandbox Code Playgroud)
使用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 …Run Code Online (Sandbox Code Playgroud)