Merge using update insert new rows

256 views Asked by At

I have below merge query where i want to update the date and for perfromance issue i am using ROWID logic.

But i would like to know does it anyhow inserts new rows ? I just want to update the table TEST_GRP and dont want any insertion of new rows.

As i am using ROWID logic for the first time i am really not sure whether it insert new rows or just update the table.

MERGE INTO TEST_GRP tgt
  USING (SELECT ID,
                ROWID r_id,
                row_number() over (partition by ID ORDER BY DT_DATE) rn
         FROM   TEST_GRP) src
    ON (tgt.rowid = src.r_id AND src.rn = 1)
WHEN MATCHED THEN
  UPDATE SET DT_DATE = to_date('01.01.2017', ''dd.mm.yyyy'')
  WHERE DT_DATE != to_date('01.01.2016', ''dd.mm.yyyy'')
  and DB_NAME = 'ARD';
2

There are 2 answers

0
Ori Marko On BEST ANSWER

You don't have to add insert clause to mergestatement as stated in the docs :

merge_update_clause ... You can specify this clause by itself or with the merge_insert_clause

And you don't have in it your code so not insert(s) will happen.

merge_insert_clause:

0
Magor_k On

It will update the rows with no problem and does not insert new rows.

At your UPDATE statment probalie it can cause you problem the quote at format mask SET DT_DATE = to_date('01.01.2017', ''dd.mm.yyyy'') DT_DATE != to_date('01.01.2016', ''dd.mm.yyyy'')