This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a script for comparing the table difference in SQL Server.
For comparing or taking the difference of tables is a very common task for most of the database developers.
So you guys can access the below simple demonstration on this.
Create two sample tables:
1 2 3 4 |
CREATE TABLE tbl_ABC (Rno INT, Name VARCHAR(20)) GO CREATE TABLE tbl_XYZ (Rno INT, Name VARCHAR(20)) GO |
Insert sampel records:
1 2 3 4 5 6 |
INSERT INTO tbl_ABC VALUES (1,'A'),(2,'B'),(3,'D'),(4,'E') GO INSERT INTO tbl_XYZ VALUES (1,'A'),(2,'C'),(3,'D'),(4,'F') GO |
Compare and Find the table differences:
1 2 3 4 5 6 7 8 |
SELECT Rno, Name FROM ( SELECT Rno, Name FROM tbl_ABC UNION ALL SELECT Rno, Name FROM tbl_XYZ ) A GROUP BY Rno, Name HAVING COUNT(*) <> 2 |
Result:
1 2 3 4 5 6 |
Rno Name ----------- --------- 2 B 2 C 4 E 4 F |
Leave a Reply