Search code examples
oracle-databasemergeinsertsql-updaterowid

Merge using update insert new rows


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';

Solution

  • 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: