Dynamic SQL Merge Script for Generate Merge statement

One day I needed to compare the tables where the source tables were production database and the target tables were for reporting database.

Therefore I built dynamic script via T-SQL that generate MERGE statement.

If in reporting table missed any data this MERGE statement inserted them from production table.

The biggest problem is in use MERGE, you can have problem if on the table is not the identity column, REFERENCE constraint, etc... So, It's very limited, but sometimes can be useful.

HERE is my Dynamic SQL Merge Script:

As you can see, you have to write your table name with schema and database name in @dst and @src varibles.

See you next time guys!

MERGE Statement