Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Merge - execute insert or update when matched depdenidng on column value

user5716448May 19 2021 — edited May 19 2021

Hi,
version 11.2.0.3
Wonder how best the following can be achieved?
Have a merge statement and when matched we execute an update on target table if finalised column on target not already set to TRUE.
requirements below.
Have input rows need to be procesesd into target table.

  1. If no row found for pk on tgt write new row on tgt
  2. If row found on tgt for pk but not yet finalised i.e. column on target called finalised != TRUE, then update existing attributes on tgt
  3. If row found but existing trg.finalised column = TRUE, write a new row on target with new pk and leave existing row untouched.

Currently using a merge to cater for 1 and 3 but would like to add option for 2 inside same merge if possible but doesnt seem.

Can this all be achieved inside the same merge?

Is there better way of achieving it.
currently below o.k.
WHEN MATCHED
THEN UPDATE
SET tgt."COL_A"= SRC.COL_A

WHERE tgt."Finalised" != 'TRUE'

WHEN NOT MATCHED
THEN INSERT...

Would like to add extra bit below but when try add doesn't like it.

WHEN MATCHED
THEN INSERT...

WHERE tgt."Finalised" = TRUE

Thanks

This post has been answered by mathguy on May 21 2021
Jump to Answer
Comments
Post Details
Added on May 19 2021
11 comments
6,077 views