SQL MERGE statement example for account management in a database

2.Merging the table of accounts whose type has been changed into the Accounts table, if their type has changed then it updates the AccountType and adds new accounts where they do not exist.
MERGE INTO Accounts a
USING TempAccounts t
ON (a.AccountID = t.AccountID)
WHEN MATCHED AND a.AccountType != t.AccountType THEN
UPDATE SET a.AccountType = t.AccountType
WHEN NOT MATCHED THEN
INSERT (AccountID, CustomerID, AccountType, Balance, AccountCreationDate)
VALUES (t.AccountID, t.CustomerID, t.AccountType, t.Balance, t.AccountCreationDate);


3.Using the MERGE statement to synchronize transaction details from a temporary transactions table with the Transactions table update the Amount if the transaction exists in the main table otherwise add new data as a new record.
MERGE INTO Transactions t
USING TempTransactions temp
ON (t.TransactionID = temp.TransactionID)
WHEN MATCHED THEN
UPDATE SET t.Amount = temp.Amount
WHEN NOT MATCHED THEN
INSERT (TransactionID, AccountID, tDate, Amount, TransactionType)
VALUES (temp.TransactionID, temp.AccountID, temp.tDate, temp.Amount, temp.TransactionType);


4.Merge data from table containing account creation updates, in the Accounts table updating fields with the date of account creation where missing or incorrect and inserting new accounts with the correct date of account creation where not present.
MERGE INTO Accounts a
USING TempAccounts t
ON (a.AccountID = t.AccountID)
WHEN MATCHED
AND
(a.AccountCreationDate IS NULL OR a.AccountCreationDate != t.AccountCreationDate)
THEN
UPDATE SET a.AccountCreationDate = t.AccountCreationDate
WHEN NOT MATCHED THEN
INSERT (AccountID, CustomerID, AccountType, Balance, AccountCreationDate)
VALUES (t.AccountID, t.CustomerID, t.AccountType, t.Balance, t.AccountCreationDate);

Explanation
The MERGE SQL statement merges data from the temp table named TempAccounts into Accounts. It checks whether an account which already exists within a given temp table, like this example's provided TempAccounts already exists in Accounts by its identifier number, the AccountID. If a match exists and AccountCreationDate is NULL or incorrect then either NULL or varies from the date in the TempAccounts table the query updates AccountCreationDate in the Accounts table based on the correct date found in the TempAccounts table. This way, the Accounts table always contains the accurate date for record creation. If not available means the account cannot be identified in the accounts table. A new record is added to the table by taking AccountID, CustomerID, AccountType, Balance and AccountCreationDate from TempAccounts. In this way, it would gather all the accounts that are wrong or absent in this account and therefore would be absent from it also. Thus, it maintains the integrity of data in both the tables simultaneously and, therefore keeps accuracy in the account management system


Previous Topic:-->>DML DELETE For Banking Account || Next topic:-->>Manage index,synonyms and sequences


DML INSERT for BankingDML UPDATE for Banking
Other Topics for Account Management
Joins With Group by Having Equi JoinJoins with Subqueries Self Join Outer Join