Skip to Main Content
  • Questions
  • Merge statement is throwing ORA-08006 error

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Mohammed Nawshard.

Asked: March 12, 2018 - 11:04 am UTC

Last updated: March 13, 2018 - 2:36 am UTC

Version: 11.2.0.4

Viewed 10K+ times! This question is

You Asked

We have a ETL job with merge statement. It was working fine earlier. There was a node eviction on last week and due to that the DB was bounced. After bouncing the DB the procedure started failing with ORA-08006 error. Same error is appearing when we try to run the statement manually. Below is the statement:

DECLARE
LV_SR_SYSTEM_ID NUMBER;
LV_SR_SYS_ID NUMBER;
BEGIN
SELECT DISTINCT SOURCE_SYSTEM_ID
INTO LV_SR_SYSTEM_ID
FROM DM_SOURCE_ENTITY
WHERE UPPER(SOURCE_SYSTEM_NAME)='CLSSC';
SELECT DISTINCT SOURCE_SYSTEM_ID
INTO LV_SR_SYS_ID
FROM DM_SOURCE_ENTITY
WHERE UPPER(SOURCE_SYSTEM_NAME)='INTREPID';


MERGE INTO DM_SERVICE_ORDER A USING
(SELECT
A.ID AS SOF_ID,
NULL AS ID,
B.SALES_ORDER_ID AS ORDER_ID,
B.ORDER_MANAGER AS ORDER_MANAGER,
B.ORDER_STATUS AS ORDER_STATUS ,
B.ORD_TYPE AS ORDER_TYPE,
B.MDD AS MDD,
LV_SR_SYSTEM_ID AS SOURCE_SYSTEM_ID
FROM (select id,service_order_id,row_number() over (partition by service_order_id
order by id desc ) rn
from f_service_order
where service_order_id is not null
) A,
TBL_BI_CLA_SVC_ORD B
WHERE A.SERVICE_ORDER_ID =B.SALES_ORDER_ID
AND A.RN=1
) B
ON (A.SOF_ID=B.SOF_ID AND A.ORDER_ID=B.ORDER_ID AND A.SOURCE_SYSTEM_ID=B.SOURCE_SYSTEM_ID
AND (A.ID IS NULL AND B.ID IS NULL)
)
WHEN MATCHED THEN
UPDATE SET
-- A.ORDER_ID=B.ORDER_ID ,
A.ORDER_MANAGER=A.ORDER_MANAGER,
A.ORDER_TYPE=A.ORDER_TYPE,
A.MDD=B.MDD,
-- A.SOURCE_SYSTEM_ID=SOURCE_SYSTEM_ID,
A.EDW_UPDATE_DATE=SYSDATE

WHEN NOT MATCHED THEN
INSERT (
A.ROW_ID,
A.SOF_ID,
A.ID,
A.ORDER_ID,
A.ORDER_MANAGER,
A.ORDER_STATUS,
A.ORDER_TYPE,
A.MDD,
A.SOURCE_SYSTEM_ID,
A.EDW_UPDATE_DATE,
A.EDW_CREATE_DATE
)
VALUES
(
DM_SERVICE_ORDER_SEQ.NEXTVAL,
B.SOF_ID,
B.ID ,
B.ORDER_ID,
B.ORDER_MANAGER,
B.ORDER_STATUS,
B.ORDER_TYPE,
B.MDD,
B.SOURCE_SYSTEM_ID,
SYSDATE,
SYSDATE
);
COMMIT;
END;

Error report:
ORA-08006: specified row no longer exists
ORA-06512: at line 16
08006. 00000 - "specified row no longer exists"
*Cause: the row has been deleted by another user since the operation began
*Action: re-try the operation

and Connor said...

If a row re-locates during the merge operation, you can encounter this.

Here's a cut-down example

SQL> create table t (pk number primary key, x number)
  2      partition by list (pk)
  3      (partition p1 values(1),
  4       partition p2 values(2)
  5      )
  6  enable row movement;

Table created.

SQL>
SQL> insert into t values (1, 1);

1 row created.

SQL> commit;

Commit complete.

SQL>
SQL> merge into t
  2  using (select 1 idx, 2 new_val from dual
  3         connect by level <= 2
  4        ) u
  5  on (t.x = u.idx)
  6  when matched then
  7    update set pk=new_val;
merge into t
           *
ERROR at line 1:
ORA-08006: specified row no longer exists


SQL>
SQL>



So the first row from 'u' was:

- I got a match on the row, which is in partition p1
- I updated the row to have pk=2, which *moved* the row into partition p2

The the second row from 'u' was:

- I got a match on the row, which was in partition p1 at the *start* of the MERGE, and is no longer there (it has moved to P2).

Hence the error. You can see it occurs when we "revisit" a row that we've already encountered in the merge. In my case, I have two rows in the merge set which both link back to the same primary key.

Avoid revisits and you should be ok.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

To requester

Gh, March 13, 2018 - 8:35 am UTC


-- A.SOURCE_SYSTEM_ID=SOURCE_SYSTEM_ID, 

Is it failing with ou without this?
If with what alias for ?a or b?

Second. What is the key partition of table?

Nawshard, April 13, 2018 - 6:41 am UTC

Hi Tom,
Issue got fixed. Tank you very much.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library