Updated March 6, 2023
Introduction to DB2 merge
DB2 MERGE statement is used to perform multiple operations on a particular table that is considered as the main or target table. The operations that are needed to be performed on that table depend on whether the records of that table are matching with the reference table also called as the source table.
The match is made on the basis of certain conditions and values that are maintained in both tables. These conditions can be either a single condition or even multiple conditions that are separated by using the logical operators like AND and OR operators in the query statement. Further, the MERGE statement also allows performing certain operations when records are matched and some other operations when records aren’t matched.
In this article, we will study the syntax of the MERGE statement in DB2 and its implementation with the help of multiple examples demonstrating the usage of the MERGE statement.
Syntax
The syntax of the MERGE statement used in DB2 is as shown below –
MERGE INTO name of the table/ name of the view
USING (table reference which will act as a source table)
ON match condition that is specified for matching the records of both the tables
WHEN MATCHED/ NOT MATCHED condition
THEN any operation that you want to perform on condition being evaluated to true
[WHEN NOT MATCHED / MATCHED
THEN any operation that you wish to perform on condition being evaluated to true
,....]
In the above syntax, we have the MERGE INTO clause used for merging the main or target table with respect to changes that are being made to the source or reference table. We can also use views in place of tables for target and source ones. Further, we can have as many WHEN MATCHED and NOT MATCHED conditions as required for performing different operations as shown above in the syntax by adding ….
Usage
We can make the use of the MERGE statement in DB2 in any of the application programs by embedding it or it can be invocated with the help of dynamic SQL statements which are nothing but the statements that can be executed and are prepared dynamically on the run time depending on the requirement. We can make use of MERGE statements with tables as well as views. Whenever we try to do the operations on the views in the MERGE statement, the corresponding reflection of the operations can be seen in the tables associated with the views ass well.
Example
Let us consider one example where we will be using the MERGE statement to update the table named bank_account_balance which is the target table and the main table which stores the data related to all the accounts and the balance of the corresponding account ids. This table needs to be updated whenever the table sum_transaction_amount which stores the data related to all the transactions that are made on all the accounts whenever any transaction is performed.
What we need to do here is that whenever any records are matched from the two tables based on the same account id then we have to update the balance of the target table which stores the balance of that account which will be dependent on the sum of the amount of the transaction that is being made for that account in the transaction table. If we don’t find any match in the bank account balance table, then we will insert a new record in the main table for that particular account whose entry is found in the sum transaction amount table.
In this case, we can make the use of MERGE statement in the following way –
MERGE INTO bank_account_balance AS bab
USING (SELECT account_id, sum(amount) sum_transaction_amount FROM account_transaction
GROUP BY account_id) AS at
ON bab.account_id = at.account_id
WHEN MATCHED THEN
UPDATE SET
balance = bab.balance + at.sum_transaction_amount
WHEN NOT MATCHED THEN
INSERT
(account_id, balance)
VALUES (at.account_id, at.sum_transaction_amount);
The execution of the above MERGE statement gives the following output
Consider one more example where we will maintain the stock and inventory of a particular company as and when the orders and items get delivered. We have to update the stock according to the items being delivered. If an item is already present in the stock of the company, we will increase the quantity of that particular item in the stock database table with the help of an update query. In case if no such item is present in the company, then a new entry is put in the stock table of the database. Both these operations can be done together by using the MERGE statement with stock is the main or target table and the delivered_order as the source or reference table as shown below
MERGE INTO stock AS in
USING (SELECT item_no, details, number_of_items FROM delivered_order
WHERE delivered_order.item_no IS NOT NULL) AS do
ON (in.item_no = do.item_no)
WHEN MATCHED THEN
UPDATE SET
details = do.details,
qty = in.qty + do.number_of_items
WHEN NOT MATCHED THEN
INSERT
(item_no, details, qty)
VALUES (do.item_no, do.details, do.number_of_items)
The execution of the above statement gives the following output –
Now, we will have a look at an example where we are maintaining the details of all the workers. And there is a main table named worker_details which will act as a target table. There is one more reference table called worker_logs which will act as the source table. Whenever any of the changes is made in the worker_logs if there is a matching record for the same worker in worker_details then we should update the mobile number and workplace for the worker in the worker details table. If no match is found then we can insert a new entry for that worker in the worker_details table using the merge statement shown below –
MERGE INTO worker_details AS w
USING (SELECT worker_id, mobile_no, workplace
FROM (SELECT worker_id, mobile_no, workplace,
ROW_NUMBER() OVER (PARTITION BY worker_id
ORDER BY log_time DESC) rn
FROM worker_logs) AS nt
WHERE rn = 1) AS l
ON w.worker_id = l.worker_id
WHEN MATCHED THEN
UPDATE SET
(mobile_no, workplace) =
(l.mobile_no, l.workplace)
WHEN NOT MATCHED THEN
INSERT
(worker_id, mobile_no, workplace)
VALUES (l.worker_id, l.mobile_no, l.workplace)
The execution of the above MERGE statement gives the following output –
Conclusion – DB2 merge
We can make use of the MERGE statement in DB2 to perform different transactions on the target table based on the operations that are being performed on the source table in order to synchronize the source and target tables with each other.
Recommended Articles
This is a guide to DB2 merge. Here we discuss the syntax of the MERGE statement in DB2 and its implementation with the help of multiple examples demonstrating the usage of the MERGE statement. You may also have a look at the following articles to learn more –