Thursday, 26 September 2019

Oracle MERGE statement


https://www.oracletutorial.com/oracle-basics/oracle-merge/

Summary: in this tutorial, you will learn how to use the Oracle MERGE statement to perform an update or insert data based on a specified condition.

Introduction to the Oracle MERGE statement

The Oracle MERGE statement selects data from one or more source tables and updates or inserts it into a target table. The MERGE statement allows you to specify a condition to determine whether to update data from or insert data into the target table.
The following illustrates the syntax of the Oracle MERGE statement:
Let’s examine the MERGE statement in detail:
First, specify the target table (target_table) which you want to update or insert into in the INTO clause.
Second, specify the source of data (source_table) to be updated or inserted in the USING clause.
Third, specify the search condition upon which the merge operation either updates or inserts in the ON clause.
For each row in the target table, Oracle evaluates the search condition:
  • If the result is true, then Oracle updates the row with the corresponding data from the source table.
  • In case the result is false for any rows, then Oracle inserts the corresponding row from the source table into the target table.
The MERGE statement becomes convenient when you want to combine multiple INSERTUPDATE, and DELETE statements in a single operation.
Because the MERGE is a deterministic statement, you cannot update the same row of the target table multiple times in the same MERGE statement.
You can add an optional DELETE WHERE clause to the MATCHED clause to clean up after a merge operation. The DELETE clause deletes only the rows in the target table that match both ON and DELETE WHERE clauses.

Oracle MERGE prerequisites

To execute the MERGE statement, you must have the INSERT and UPDATE object privileges on the source tables. If you use the DELETE clause, you must also have the DELETE object privilege on the target table.

Oracle MERGE example

Suppose, we have two tables: members and member_staging.
We insert a new row to the members table whenever we have a new member. Then, the data from the members table is merged with data of the member_staging table.
The following statements create the members and member_staging tables:
The following INSERT statements insert sample data into the members and member_staging tables:
When updating data from the members table to member_staging table, we should perform the following actions:
  • We update the rows with member id 1, 3, 4, and 6 because the rank or the last name of these members in these tables are different.
  • We insert the rows with member id 7 to 10 are because these rows exist in the members table but not in the member_staging table.
In total 8 rows should be merged.
Oracle MERGE example
The following is the MERGE statement that performs all of these actions in one shot.
The merge statement compares each row in the members table with each row in the member_staging table based on the values in the member_id columns (see the ON clause above).
If the values in member_id columns of both tables are equal, the MERGE statement updates the first name, last name, and rank from the members table to the member_stagingtable only if the values of first name, last name, or rank columns of both tables are different.
Otherwise, it inserts the row from the members table into the member_staging table.
Oracle returned 8 rows merged as expected.
In this tutorial, you have learned how to use the Oracle MERGE statement to update or insert data based on a specified condition.

No comments:

Post a Comment