简体   繁体   中英

Table Diff in SQL Server 2000

I'm using SQL Server 2000, and given aa 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?

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.

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

Marc

是的,同意Marc(一种专用工具,效果最好),例如适用于SQL Server的Volpet Table Diff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM