Oracle MERGE语句错误(ORA-30926)

时间:2015-02-20 19:36:53

标签: oracle oracle11g

我继承了在Oracle 11g上运行的Oracle脚本,该脚本包含以下MERGE语句:

MERGE INTO MainTable PR
     USING IncrementalTable PRA
       ON (PR.contract = PRA.contract
       and PRA.memberType = 'Parent' )
     WHEN MATCHED THEN
       UPDATE SET PR.address1 = PRA.address1,
                  PR.city = PRA.city,
                  PR.state = PRA.sate,
                  PR.zipCode = PRA.zipCode,
                  PR.countryCode = PRA.countryCode
WHERE address1 IS NULL
  AND PR.memberType <> 'Parent'
;

据我所知,这只是在IncrementalTable中从父地址更新MainTable中的孩子的地址。不幸的是,当我运行该语句时,它会抛出以下错误:

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

因此,看起来它无法在IncrementalTable中找到与之更新的明显匹配。查询数据,似乎是这种情况:

select contract,
       memberType,
       count(*)
  from IncrementalTable
 group by contract,
       memberType
having count(*) > 1
;

CONTRACT               MEMBERTYPE   COUNT(*)
---------------------- ---------- ----------
1119839490             PARENT              2
7271122516             PARENT              2
1004798721             PARENT              2

查看其中一份合约的详情:

select *
  from IncrementalTable
 where contract = '1119839490'
   and memberType = 'Parent'
;


CONTRACT               MEMBERTYPE ADDRESS1          CITY                           STATE ZIPCODE   COUNTRYCODE
---------------------- ---------- ----------------  ------------------------------ ----- -------   -----------
1165439488             Parent     1234 Dorioth St   Orlando                        FL    32825     USA
1165439488             Parent     1234 Dorioth St   Orlando                        FL    32825     USA

那么,我怎样才能合并来自IncrementalTable的DISTINCT匹配?

谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

第一个(也是最明显的)答案是清理数据,这样就不会有重复数据。您提供的样本数据似乎可能是错误的结果。

对于merge语句,如果数据实际上是纯粹重复的,您只需使用distinct

MERGE INTO maintable pr
USING      (SELECT DISTINCT contract,
                            membertype,
                            address1,
                            city,
                            state,
                            zipcode,
                            countrycode
            FROM   incrementaltable pra
            WHERE  pra.membertype = 'Parent')
ON         (pr.contract = pra.contract)
WHEN MATCHED THEN
   UPDATE SET pr.address1 = pra.address1,
              pr.city = pra.city,
              pr.state = pra.sate,
              pr.zipcode = pra.zipcode,
              pr.countrycode = pra.countrycode
      WHERE      address1 IS NULL AND pr.membertype <> 'Parent'