Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to compare the data in 2 tables?

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-17 : 17:22:12
Hi there,

I have tabel1 and table2 which have exactly the same structures. I mean same cols and data types, etc. But table1 populated from SSIS import and table2 are filled from some ASP.NET codes based input. Now I need to compare if these two tables have the same data, here same means same rows and each field on a row should have same data. how can I do this? I mean compare them by sql secripts. Thanks in advance.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-02-17 : 17:25:31
Easiest method in SQL 2005 or higher:

SELECT * FROM table1
INTERSECT
SELECT * FROM table2

This will compare all columns in all rows in both tables and return those rows that exist in both. You can find more information on INTERSECT (and EXCEPT) here:

http://msdn.microsoft.com/en-us/library/ms188055.aspx

If you have more complicated needs please post your table structure (DDL statement), some example data, and the results you're expecting.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-17 : 17:52:48
Very cool, I just tested it works and I can use it for all cols and only one col. Thanks robvolk!!!
Go to Top of Page
   

- Advertisement -