views:

111

answers:

3

I'm using SQL Server 2000, and given a a set of data (with unique ID's), I want to figure out the diffs with rows in the database, matching on the unique IDs. I'm also trying to keep it as flexible as possible.

I can use C#/VB in the front end to take the parameters, or even return things. Maybe passing in XML and getting XML in return?

For example, I want a function that calls:

// returns the columns that are different
func( A, B, C ) {

}

Any ideas?

+2  A: 

I'd go with an existing "data diff" tool like

Marc

marc_s
I'd also upgrade from SQL Server 2000 before these great tools stop supporting it.
John Saunders
+3  A: 

There's a cool trick you can use with UNION:

SELECT MAX(tbl) AS TABLE_NAME, unique_key
FROM (
    SELECT 'table1' AS tbl, unique_key
    FROM table1
    UNION ALL
    SELECT 'table2' AS tbl, unique_key
    FROM table2
) AS X GROUP BY unique_key
HAVING COUNT(*) = 1

This will show where one side or the other has rows which the other doesn't have.

This can be expanded to do more, obviously.

Alternatively, you can do an INNER JOIN (matches on keys where data is different), LEFT JOIN (key missing on one side) and RIGHT JOIN (key missing on the other) and UNION them all together.

I actually have a utility SP (it uses one or the other method depending on what options you need) which will compare any two tables and has options for setting which columns are considered the part of keys, which columns to ignore, restrict each side to a subset, etc. It even has an option to write the differences to a table.

Cade Roux
A: 

Yeah agree with Marc, a specialized tool works best, something like Volpet Table Diff for SQL Server

L. De Leo