Search

Thursday, July 28, 2011

Comparing 2 Results Sets to see if they are identical


SELECT CASE WHEN COUNT(*) = 0 THEN 'Same' ELSE 'Different' END
FROM (
        (
        SELECT * FROM Table1 
        EXCEPT
        SELECT * FROM Table2
        ) 
        UNION 
        (
        SELECT * FROM Table2
        EXCEPT
        SELECT * FROM Table1 
        )
) AS TempTable

This query gets all the rows that are in Table 1 but not Table 2, then UNIONS all rows that are in Table 2 but not Table 1.  If there's zero rows for both, the result sets must be the same.

No comments:

Post a Comment