Is it possible to get number of rows inserted and updated separately in merge query?
SQL%COUNT
gives total number of rows merged.
Is it possible to get number of rows inserted and updated separately in merge query?
SQL%COUNT
gives total number of rows merged.
SQL%COUNT gives total number of rows merged.
sql℅rowcount
gives you the row count of the total rows affected by the MERGE statement. It cannot decompose the INSERT/UPDATE/DELETE
row count.
But you could decompose sql%rowcount
for Merge by writing
With knowledge of total number of rows before and after merge
, you can utilize this information in simple arithmetic computation:
┌───────────────┐
▲ ▲ │ │
│ │ │ │
│ │ │ │
│ │ │ │
count_before│ │ │ │
│ │ │ │
│ │ │ │
│ │ │UUUUUUUUUUUUUUU│ ▲ ▲
│ │ │UUUUUUUUUUUUUUU│ │rows_updated │
▼ │ │UUUUUUUUUUUUUUU│ ▼ │
│ │IIIIIIIIIIIIIII│ ▲ │sql%rowcount
count_after│ │IIIIIIIIIIIIIII│ │rows_inserted │
│ │IIIIIIIIIIIIIII│ │ │
▼ │IIIIIIIIIIIIIII│ ▼ ▼
└───────────────┘
select count(*) into count_before from table_name;
merge into table_name ...;
rows_merged := sql%rowcount;
select count(*) into count_after from table_name;
rows_inserted := count_after - count_before;
rows_updated := rows_merged - rows_inserted;
(credit to: asciiflow.com)
A solution could be that whenever you insert or update one row.. you insert a code in your table, for example:
code=1 --> if insert
code=2 --> if update
so use that condition in where clause when you need count
add a delete_count = count_before - count_after. would change the result for insert update counts if count_after is less than count_before.
case for counting deletes with merge condition seperate statement to know how many were deleted. sql%rowcount could do that.