TSQL How To Synchronize Between 2 Tables
This query work to find out any difference between 2 tables and return Only The Difference Row, Or Not There Row.
if the union all Group Count return 2 meaning it’s the same ,if 1 Than there’s difference. You can put on temporary table
SELECT MIN(tableName),Column1,Column2 .. N
FROM
(
--sync
SELECT 'Table 1' AS tableName,Column1,Column2 .. N
FROM
Table1
UNION ALL
SELECT 'Table 2' AS tableName,Column1,Column2 .. N
FROM
Table2
)
tmp
GROUP BY Column1,Column2 ..N
--1 means have difference, 2 means identical
HAVING COUNT(*)=1
ORDER BY Column1,Column2 ..N
After That , Do your Insert Update Delete Logic From The Results.