合并到错误ORA-30926:无法在源表中获得稳定的行集

时间:2017-09-04 09:42:02

标签: sql oracle

我尝试按顺序运行接下来的2个查询。第一个完美运行,第二个抛出

  

ORA-30926:无法在源表中获得一组稳定的行

我在网上搜索了一个解决方案,但我无法复制它以供我查询。有人可以帮帮我吗?

查询1:

merge into sdc_compare_person dcip
using (
select anumber, position, character
from sdc_diakrietposities_cip
where kind = 'Surname'
) x
on (dcip.sourcekey = x.anumber)
when matched then update set
dcip.GESVOR = substr(dcip.GESVOR, 1, x.position - 1) ||
                x.character ||
                substr(dcip.GESVOR, x.position + 1, length(dcip.GESVOR)-x.position)
;

188 rows merged.

查询2:

merge into sdc_compare_person dcip
using (
select anumber, position, character
from sdc_diakrietposities_cip
where kind = 'Lastname'
) x
on (dcip.sourcekey = x.anumber)
when matched then update set
dcip.GESNAM_D = substr(dcip.GESNAM_D, 1, x.position - 1) ||
                x.character ||
                substr(dcip.GESNAM_D, x.position + 1, length(dcip.GESNAM_D) - x.position)
;

SQL Error: ORA-30926: Unable to get a stable set of rows in the source tables

2 个答案:

答案 0 :(得分:2)

你总是可以使用普通的更新,它不像MERGE那么优雅,但应该有效:

UPDATE sdc_compare_person dcip
SET dcip.GESNAM_D = (
    SELECT substr(dcip.GESNAM_D, 1, x.position - 1) ||
           x.character ||
           substr(dcip.GESNAM_D, x.position + 1, length(dcip.GESNAM_D) - 
                  x.position)
    FROM sdc_diakrietposities_cip x
    where kind = 'Lastname'
      AND dcip.sourcekey = x.anumber
)
WHERE dcip.sourcekey  IN (
   select anumber
   from sdc_diakrietposities_cip
   where kind = 'Lastname'
);

答案 1 :(得分:1)

从评论到问题,很明显作者想多次更新同一条记录 当然,当尝试通过合并构造来实现时,这不能超过ORA-30926 在纯粹的oracle sql中做这样的事情很难或不可能,但是用pl / sql函数很容易做到这一点。
例如:

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="columnchart_ticketperday"></div>

像这样使用:

create or replace function replace_chars(p_str varchar2, p_id number, p_kind varchar2) return varchar2 as
                                l_str varchar2(32767):=p_str;
begin
    for u in (select u.position, u.character  from sdc_diakrietposities_cip u 
                  where u.anumber=p_id and u.kind=p_kind order by u.position) loop
        if (u.position >= 1 or u.position <= length(l_str)) then
            l_str:=substr(l_str, 1, u.position-1)|| u.character || substr(l_str, u.position+1);
        end if;
    end loop;
    return l_str;
end;

我建议在运行之前备份你的桌子。