"None can stop the rising sun, clouds can hide for a while........" -Ravi

Friday, February 20, 2009

Compare two complete ROWS( all columns of each row) in two seperate TABLES

If you have two separate tables with same primary key and want to check the updates in the other table,using BINARY_CHECKSUM would be the best answer. But need to careful about column data types. Column Data types shouldn't be text, ntext, image, XML, and cursor.

Here is T-SQL query to compare two rows from different table:

Take ORDERS1 as one table and ORDERS2 as other table

SELECT ORDERS1.PRIMARYKEY_ID
FROM
(SELECT PRIMARYKEY_ID, BINARY_CHECKSUM(*) AS CHECKVALUE FROM ORDERS1) AS CHECKSUMTABLE1
INNER JOIN
(SELECT PRIMARYKEY_ID, BINARY_CHECKSUM(*) AS CHECKVALUE FROM ORDERS2) AS CHECKSUMTABLE2
ON CHECKSUMTABLE1.PRIMARYKEY_ID= CHECKSUMTABLE2.PRIMARYKEY_ID
WHERE CHECKSUMTABLE1.CHECKVALUE <> CHECKSUMTABLE2.CHECKVALUE

No comments: