The SQL Server MERGE command is the combination of INSERT, UPDATE and DELETE commands consolidated into a single statement. Here is how to get started with the use MERGE into in SQL Server:

  1. Start off by identifying the target table which will be used in the logic.
  2. Next identify the source table which will be used in the logic.
  3. Determine the appropriate search conditions in the ON clause in order to match rows.
  4. Specify logic when records are matched or not matched between the target and source i.e. comparison conditions.
  5. For each of these comparison conditions code the logic. When matched, generally an update condition is used. When not matched, generally an insert or delete condition is used.

However, SQL Server provides the  MERGE statement that allows you to perform three actions at the same time. The following shows the syntax of the  MERGE statement:

Example

You may like How to Split comma delimited string into a Table in SQL Server.

Use Merge Into in SQL Server

Leave a Comment