Merge
Merge
all within the same statement. You specify a "Source" record set and a "Target" table,
and the join between the two. You then specify the type of data modification that is to
occur when the records between the two data are matched or are not matched. MERGE
is very useful, especially when it comes to loading data warehouse tables, which can be
very large and require specific actions to be taken when rows are or are not present.
In this example I will take a Products table as target table and UpdatedProducts as a
source table containing updated list of products. I will then use the MERGE SQL
command to synchronize the target table with the source table.
First Let's create a target table and a source table and populate some data to
these tables.
Next I will use the MERGE SQL command to synchronize the target table with the
refreshed data coming from the source table.
When the above is run this is the output. There were 2 updates, 1 delete and 1 insert.
If we select all records from the Products table we can see the final results. We can see
the Coffee rate was updated from 20.00 to 25.00, the Muffin rate was updated from
30.00 to 35.00, Biscuit was deleted and Pizza was inserted.
Notes