我有一个合并查询,如下所示:
merge into table1
using table2
on (table1.column1 = table2.column1)
when mateched then
update
set column2 = table2.column2;
现在它给了我错误,例如:无法在源表中获取一组稳定的行
现在的问题是源表 table2 具有同一列 1 值的多条记录,而表 1 的每个列 1 值只有一条记录。我需要所有表2来更新表1。你能在这里帮忙吗?
" 问题是源表2,同一列1值有多个记录">
您需要确保子查询仅返回每个值 column1
的一行。只有您知道需要应用什么确切的业务规则,但它可能是这样的:
merge into table1
using ( select column1, max(column2) as column2
from table2
group by column1 ) t2
on (table1.column1 = t2.column1)
when matched then
update
set column2 = t2.column2;
"我需要表 2 中的所有记录来更新表 1。原因是我在 table1 上有一个触发器,它捕获所有事务(插入/更新(并加载到另一个历史记录表。">
您不能使用 MERGE。事实上,你可能不得不去程序化:
begin
for t2rec in (select column1, column2
from table2
order by column1, column2 )
loop
update table1
set column2 = t2rec.column2
where column1 = t2rec.column1;
end loop;
end;
您需要对循环语句进行排序,以确保table1
的结束状态是正确的。
或者,您可以禁用日记触发器并执行以下操作:
insert into table1_journal
select table1.column1
, table2.column2
from table1
join table2
on table1.column1 = table2.column1
/
然后执行合并或单行更新。