原因是:在使用merger into 时,有重复数据导致的
参考:
最好的方法是用merge语法:
merge into tab1
using tab2
on(tab1.id=tab2.id)
when matched then
update set tab1.val = tab2.val
同样,如果tab2中有多条对应tab1中一条的情况会出错:ORA-30926: unable to get a stable set of rows in the source tables
比如在tab2中再插入一条 insert into tab2 values(2,'xxxx')
可以通过在using中的subquery中将重复记录过滤来避免这种错误,
merge:
merge into tab1
using (select * FROM tab2 X WHERE X.ROWID =
(SELECT MAX(Y.ROWID) FROM tab2 Y WHERE X.ID = Y.ID)) tab2
on(tab1.id=tab2.id)
when matched then
update set tab1.val = tab2.val
但是考虑到关联查询效率问题,可以使用oracle分析函数去重后merge
MERGE INTO YTRPT.T_EXP_CHECK_STATION P
USING (SELECT ORG_CODE, WAYBILL_NO
FROM (SELECT K.ORG_CODE,
K.WAYBILL_NO,
ROW_NUMBER() OVER(PARTITION BY K.WAYBILL_NO ORDER BY K.CREATE_TIME DESC) AS RW --去除重复取出最新的那条记录,否则merge会出错
FROM YTEXP.T_EXP_OP_RECORD_HANDON K
WHERE K.CREATE_TIME >= P_START_TIME
AND K.CREATE_TIME < P_END_TIME + 10
AND K.OP_CODE IN ('710', '711') --派件扫描
AND K.STATUS > 0
AND K.AUX_OP_CODE <> 'DELETE')
WHERE RW = 1) TEMP
ON (P.WAYBILL_NO = TEMP.WAYBILL_NO AND P.RPT_DATE = P_START_TIME)
WHEN MATCHED THEN
UPDATE SET P.SIG_ORG_CODE = TEMP.ORG_CODE;