USE OF MERGE STATEMENT IN MSSQL

,

The MERGE statement in Microsoft SQL Server is a powerful and flexible command that allows you to perform multiple operations (INSERT, UPDATE, DELETE) in a single statement based on a specified condition.

It is very helpful when doing the ETL operations like UPSERTS (Inserts and Update) and data synchronization between the source and target data.

Basic Syntax:

Example:

Let us see how merge statement works in real time.

For example, we often have different databases for development, test and production in real time, and the tables should be always synchronized. We can use MERGE statement to achieve this goal.

For that, let us first create some source and target tables to mimic the real time scenario.

Output:

If we select from the source table, we will see the records inserted.

If we select from the target table, there will be no rows for now

Below, we can see the possibilties we have using MERGE statements.

1. Basic MERGE statement:

Let us create a Merge statement using our syntax given above.

Let us execute the above Merge statement and we will see that the INSERT statement has been executed, and the target table would have

Now, if we select from the target table, we have the same number of rows as in the target table

Now let us modify a record in the source to see how the update in the MERGE statement works.

--Update an existing value in the source table
UPDATE SRC_SALES_SAMPLES SET SALES_AMOUNT = 2000.00 WHERE SALES_ID = 2;

When we execute the above update statement, we will have a mismatch between source and table.

Now if we run the above MERGE statement, we get the record in the target table updated.

Now, let us delete a record from the source table and run the MERGE statement again.

Before running the MERGE statement, the source and target table looks like this,

After running the MERGE statement, we get the following result.

2. Using Additional Conditions:

If we want to filter the rows by adding additional conditions, we can also do that.

The below example shows how we can apply filters.

Running the above query will result in inserting entries which are only having SALES_AMOUNT > 200 in the source table.

3. Logging Changes into an Audit Table:

  • We can use the OUTPUT clause to log changes into an audit table.

To demonstrate it, let us create a sample Audit table.

Perform some update and delete operations to trigger the audit logging.

Now run the following MERGE statement, with the OUTPUT clause added in the end.

We can see all the actions performed when we select from the AuditTable.

Summary:

  • MERGE statements are used to perform ETL operations and UPSERTS effectively and gives us the opportunity to write INSERTS, UPDATES, and DELETES in a single statement.
  • MERGE statements are often use in real time for source to target data synchronization.
  • Always test the MERGE statement thoroughly, especially when dealing with complex conditions and multiple operations.
  • Ensure that your ON conditions are well-defined and won’t result in unexpected matches.
  • Consider using transactions to ensure atomicity when dealing with multiple operations.
  • Be cautious when using the DELETE clause, as it removes rows from the target table.
  • OUTPUT clause can be used to log into Audit tables.
  • Use additional conditions to narrow down the exact rows to be merged.
  • For best practices, always parameterize when inserting into the table.

References:

  1. https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver16
  2. https://www.mssqltips.com/sqlservertip/1704/using-merge-in-sql-server-to-insert-update-and-delete-at-the-same-time/

Popular posts