Oracle中如何更新多行include with-select句
How to update multiple rows include with-select sentence in Oracle
我想用另一个 table 的值更新 table_a 的 col1。
我做了一个select就是
with tmp as (
blar~
)
select col1 from table_b b, tmp t
where 1=1
and b.col2 = t.col_x
更新加入条件
table_a.col3 = table_b.col3
您没有提供足够的信息,很难提供帮助。但是结果可以通过以下方式更新:
update (
with temp as (
select col1 from table_a
)
select col1 from temp )
set col1 = 'newValue'
原理是这样创建一个可更新的视图:
update
( with tmp as ( select col2
from table_c)
select a.col1 as a_col1
, b.col1 as b_col1
, a.id as a_id
from tmp
join table_b b
on b.col2 = tmp.col2
join table_a a
on a.col3 = b.col3
) t
set a_col1 = b_col1
/
重点是:
- 我们只从视图更新一个table;
- 视图中的所有其他 table 都有主键或唯一键,因此它们保证 return 只有一行。
- 必须在视图中引用唯一列
如果更新不满足这些限制,它将抛出 ORA-01779: cannot modify a column which maps to a non key-preserved table
。
不清楚您为什么要使用 WITH 子句,但这可能会更难确保您使用的是保留密钥的 table。例如,上述查询的这个变体失败了,即使我们知道 DUAL 总是 return 一行。
update
( with tmp as ( select c.col2
from table_c c
join dual d
on d.dummy = c.col2)
select a.col1 as a_col1
, b.col1 as b_col1
, a.id as a_id
from tmp
join table_b b
on b.col2 = tmp.col2
join table_a a
on a.col3 = b.col3
) t
set a_col1 = b_col1
/
如果此解释不能帮助您找到解决方案,请编辑您的问题以提供更多详细信息。包括您收到的任何错误消息。 "i can't success" 信息不足,我们无法帮助您。
"I was use your answer that called /*+ bypass_ujvc */ "
这是一个未记录的提示,因此在生产环境中使用它非常危险。此外,似乎 Oracle removed it in 11gR2 及更高版本,因此它不会有任何影响(这就是为什么使用未记录的提示是有风险的)。你应该找到一个没有提示的解决方案。
我想用另一个 table 的值更新 table_a 的 col1。
我做了一个select就是
with tmp as (
blar~
)
select col1 from table_b b, tmp t
where 1=1
and b.col2 = t.col_x
更新加入条件
table_a.col3 = table_b.col3
您没有提供足够的信息,很难提供帮助。但是结果可以通过以下方式更新:
update (
with temp as (
select col1 from table_a
)
select col1 from temp )
set col1 = 'newValue'
原理是这样创建一个可更新的视图:
update
( with tmp as ( select col2
from table_c)
select a.col1 as a_col1
, b.col1 as b_col1
, a.id as a_id
from tmp
join table_b b
on b.col2 = tmp.col2
join table_a a
on a.col3 = b.col3
) t
set a_col1 = b_col1
/
重点是:
- 我们只从视图更新一个table;
- 视图中的所有其他 table 都有主键或唯一键,因此它们保证 return 只有一行。
- 必须在视图中引用唯一列
如果更新不满足这些限制,它将抛出 ORA-01779: cannot modify a column which maps to a non key-preserved table
。
不清楚您为什么要使用 WITH 子句,但这可能会更难确保您使用的是保留密钥的 table。例如,上述查询的这个变体失败了,即使我们知道 DUAL 总是 return 一行。
update
( with tmp as ( select c.col2
from table_c c
join dual d
on d.dummy = c.col2)
select a.col1 as a_col1
, b.col1 as b_col1
, a.id as a_id
from tmp
join table_b b
on b.col2 = tmp.col2
join table_a a
on a.col3 = b.col3
) t
set a_col1 = b_col1
/
如果此解释不能帮助您找到解决方案,请编辑您的问题以提供更多详细信息。包括您收到的任何错误消息。 "i can't success" 信息不足,我们无法帮助您。
"I was use your answer that called /*+ bypass_ujvc */ "
这是一个未记录的提示,因此在生产环境中使用它非常危险。此外,似乎 Oracle removed it in 11gR2 及更高版本,因此它不会有任何影响(这就是为什么使用未记录的提示是有风险的)。你应该找到一个没有提示的解决方案。