Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

SQL Compare Without The Price Tag

From Wiki

Jump to: navigation, search

You want to know the differences between 2 tables but don’t have the money to pay for SQL Compare by red-gate software and your trial version has expired ;-(

Let me say first of all that SQL Compare is probably the best third party product around for SQL Server (along with SQL LITESPEED by QUEST Software)

In order to see what rows are in table 1 and not in table 2 and vice versa you can do 2 left joins, 2 right joins or 1 left and 1 right join. To get the rows that are different you can use CHECKSUM

Let’s get started…

  1. --let's copy over 20 rows to a table named authors2
  2. SELECT TOP 20 * INTO tempdb..authors2
  3. FROM pubs..authors
  4.  
  5. --update 5 records by appending X to the au_fname
  6. SET ROWCOUNT 5
  7.  
  8.  
  9. UPDATE tempdb..authors2
  10. SET au_fname =au_fname +'X'
  11.  
  12.  
  13. --Set rowcount back to 0
  14. SET ROWCOUNT 0
  15.  
  16. --let's insert a row that doesn't exist in pubs
  17. INSERT INTO tempdb..authors2
  18. SELECT '666-66-6666', au_lname, au_fname, phone, address, city, STATE, zip, contract
  19. FROM tempdb..authors2
  20. WHERE au_id ='172-32-1176'
  21.  
  22. --*** The BIG SELECT QUERY --***
  23.  
  24. --Not in Pubs
  25. SELECT 'Does Not Exist On Production',t2.au_id
  26. FROM pubs..authors t1
  27. RIGHT JOIN tempdb..authors2 t2 ON t1.au_id =t2.au_id
  28. WHERE t1.au_id IS NULL
  29. UNION ALL
  30. --Not in Temp
  31. SELECT 'Does Not Exist In Staging',t1.au_id
  32. FROM pubs..authors t1
  33. LEFT JOIN tempdb..authors2 t2 ON t1.au_id =t2.au_id
  34. WHERE t2.au_id IS NULL
  35. UNION ALL
  36. --Data Mismatch
  37. SELECT 'Data Mismatch', t1.au_id
  38. FROM( SELECT BINARY_CHECKSUM(*) AS CheckSum1 ,au_id FROM pubs..authors) t1
  39. JOIN(SELECT BINARY_CHECKSUM(*) AS CheckSum2,au_id FROM tempdb..authors2) t2 ON t1.au_id =t2.au_id
  40. WHERE CheckSum1 <> CheckSum2
  41.  
  42. --Clean up
  43. DROP TABLE tempdb..authors2
  44. GO


You can run this whole script in 1 shot

Let me finish by saying that this code should just be used for quick checking. SQL Compare is far superior to this and will also generate the scripts for you to synchronize the 2 tables. So if you only need to do something like this once then use this code or download the SQL compare trial version. If you do this on a regular basis then you should seriously consider getting SQL compare

I am not being paid or endorsed in any way by red-gate, I just happen to like their product a lot and it saved me a lot of time and trouble once a developer went on vacation

This query below was submitted to me by Louis Davidson, basically a FULL OUTER JOIN instead of a UNION

  1. SELECT CASE WHEN t1.au_id IS NULL
  2. AND t2.au_id IS NOT NULL
  3. THEN 'Does Not Exist On Production'
  4. WHEN t1.au_id IS NOT NULL
  5. AND t2.au_id IS NULL
  6. THEN 'Does Not Exist In Staging'
  7. ELSE 'Data Mismatch' END,
  8. COALESCE(t1.au_id, t2.au_id) AS au_id
  9. FROM (SELECT *, BINARY_CHECKSUM(*) AS bc FROM pubs..authors) AS t1
  10. FULL OUTER JOIN (SELECT *, BINARY_CHECKSUM(*) AS bc FROM tempdb..authors2) AS t2
  11. ON t1.au_id =t2.au_id
  12. WHERE t1.au_id IS NULL
  13. OR t2.au_id IS NULL
  14. OR t1.bc <> t2.bc


Contributed by: --SQLDenis 14:11, 14 June 2008 (GMT)

Part of SQL Server Admin Hacks

408 Rating: 2.6/5 (8 votes cast)