SQL Server - Comparing Tables (Merge, Except, Intersect)

posted on Monday, February 02, 2009 8:56 PM

I had a need to compare two SQL Server table for differences between them.  I started using .NET dataset features (merge, acceptchanges, getchanges) as follows:

        Dim data1 DataSet = GetData1() 

        Dim data2 DataSet = GetData2() 

 

        Dim ds As New DataSet

        ds.Merge(data1)

        ds.AcceptChanges()

        ds.Merge(data2)

        ds.GetChanges(DataRowState.Modified)

There are a few gotcha's with the above code.  The primary problem was that both tables must have primary keys defined.  I figured ok, I could create primary keys through code for the related DataTables however I soon realized that there were duplicate rows within the tables.

SQL Server 2005 has Except and Intersect functions (http://msdn.microsoft.com/en-us/library/ms188055(SQL.90).aspx) that return distinct values by comparing the results of two queries.  The entire row is compared against another row from another table.

Except returns any distinct values from the left query that are not found on the right query.
Intersect returns any distinct values that are returned by both the query on the left and right sides.

In order to use the number and order of the columns must be the same in the queries and also the data types must be comparable. 

To return all rows in table1 that do not match exactly the rows in table2, you can use Except ...
select * from table1 except select * from table2

(likewise to find the opposite just reverse the table names above)

To return all rows in table1 that match exactly what is in table2, using Intersect...
select * from table1 intersect select * from table2

Combining the above two... (the following will return the differences)
select 'table1' as tblName, *  from
  (select * from Table1 except select * from Table2) x
union all
select 'table2' as tblName, *  from
  (select * from Table2 except select *  from Table1 ) x


If you are fortunate to have primary keys you can of course still use IN/NOT IN type queries however it seems that performance is much improved with the Except/Intersect approach.

 

category: SQL Server

Comments


# re: SQL Server - Comparing Tables (Merge, Except, Intersect)
Posted by Kasino Verzeichnis on 3/3/2010 12:12 AM
Gravatar I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post
# re: SQL Server - Comparing Tables (Merge, Except, Intersect)
Posted by Poker virtuale on 3/3/2010 5:39 AM
Gravatar Cool........It saved a lot of time for me. I was looking for something like this and it came nicely to me......Good work.Just a short explanation: We can use column names for duplicate check.I used it where i wanted to compare values of two tables TableA and TableB and insert new rows in TableA if the row exist in TableB but not in TableA. I was thinking to create a temp table but now i need not to........Thanks a lot.
Post Comment
Title *
Name *
Email
Url
Comment *
Please add 6 and 4 and type the answer here:

About Me

An engineer by training and a software developer at heart. My techniques and approaches meld engineering approaches with software technology.

Core to these principles is a systematic approach to the development of software with a strong lifecycle and process management emphasis through adoption of mature technologies.

Ten years designing heavy structural steel and concrete structures and 12 years in the software development profession have embedded strong project management and business knowledge in my approaches.

Subscribe to Rss Feed


Follow me on twitter @dyardy