暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 非确定性行集的merge语句中未引发ORA-30926

ASKTOM 2020-02-11
902

问题描述

嗨,康纳,克里斯等人,

您能否帮助我更好地理解何时对输入的非确定性行集提出ORA-30926?

高达12.1,它作为一个魅力,无论排序顺序和数量重复的行在输入ORA-30926被提出 (测试10.2.0.5,12.0,12.1)。
从12.2开始到19.4行为已更改,因此不会在每种情况下引发异常,而是合并2行。

提前谢谢你。

BR,
马丁·Z

 
drop table target purge;
drop table source purge;

create table target ( a number, b number );
create table source ( a number, b number );

insert into target values ( 1, 1 );
insert into source values ( 1, 1 );
insert into source values ( 1, 2 );
insert into source values ( 1, 3 );

--fails on ORA-30926: unable to get a stable set of rows in the source tables
merge into target
using (select * from source order by b desc) source
on (target.a = source.a)
when matched then
update set target.b = source.b;

--fails on ORA-30926: unable to get a stable set of rows in the source tables
merge into target
using (select * from source order by b asc) source
on (target.a = source.a)
when matched then
update set target.b = source.b;

--fails on ORA-30926: unable to get a stable set of rows in the source tables
merge into target
using source
on (target.a = source.a)
when matched then
update set target.b = source.b;

delete source where b = 3;

--fails on ORA-30926: unable to get a stable set of rows in the source tables
merge into target
using (select * from source order by b desc) source
on (target.a = source.a)
when matched then
update set target.b = source.b;

--2 rows merged
merge into target
using (select * from source order by b asc) source
on (target.a = source.a)
when matched then
update set target.b = source.b;

--fails on ORA-30926: unable to get a stable set of rows in the source tables
merge into target
using source
on (target.a = source.a)
when matched then
update set target.b = source.b;

rollback;

--2 rows merged
merge into target
using source
on (target.a = source.a)
when matched then
update set target.b = source.b;
复制

专家解答

这就是merge一直以来的工作方式。

我在11.2.0.4和19.3上看到完全相同的行为。除b asc从源顺序中选择 * 的语句外,所有语句都ORA-30926。

当它尝试两次更新同一行时,合并会引发ORA-30926。

如果要开始,你有

1,1

在目的地。和

1,1
1,2

源和合并按照该顺序处理源中的行。所以首先它设置

b = 1

这是它已经拥有的价值。所以这 “什么都不做”。然后下一行将其更新为2。所以只有目标中的行changes一次。

但是,如果您按照以下顺序处理源中的行:

1,2
1,1

首先你更新目标b = 2。然后应用第二行将其更新回1。这意味着该行现在已经changed twice。因此ORA-30926。

请注意,这意味着如果源中的所有行都具有相同的B值,则不会看到错误:

create table target ( a number, b number );
create table source ( a number, b number );

insert into target values ( 1, 1 );
insert into source values ( 1, 1 );
insert into source values ( 1, 1 );
insert into source values ( 1, 1 );

commit;

merge into target
using (select * from source order by b desc) source
on (target.a = source.a)
when matched then
update set target.b = source.b;

3 rows merged.
复制


除非将order by添加到源查询,否则merge可以以任何顺序从源接收行。因此,如果您在从12.1 -> 19.4升级后开始看到这种行为,那是因为merge以不同的顺序接收行。例如,因为行的物理存储在升级过程中发生了变化。

如果这在您的应用程序中引起问题,您需要:

-在源查询中添加确定性排序以避免该问题
-确保目标中每一行的源中最多只有一行
文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论