MSSQL: Checking for Equality of Tables (Hints only, please)

  • Thread starter Thread starter WWGD
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on methods to determine if two tables, T1 and T2, in MSSQL 2012 are equal. The primary suggestion is to utilize the "EXCEPT" operator to compare the tables, which should yield an empty result set if they are equal. Additional hints include considering the handling of schema differences, potential duplicates, and using the command line utility "tablediff" for comparison. Participants also discuss the importance of counting distinct records and the implications of using correlated subqueries.

PREREQUISITES
  • Understanding of MSSQL 2012 SQL syntax
  • Familiarity with the "EXCEPT" operator in SQL
  • Knowledge of handling duplicate records in SQL queries
  • Basic understanding of SQL system views, specifically information_schema.columns
NEXT STEPS
  • Research the "EXCEPT" operator in MSSQL 2012 documentation
  • Learn about the command line utility "tablediff" for table comparison
  • Explore methods for counting distinct records in SQL
  • Investigate the use of "RANK" or "DENSE_RANK" functions for ordering records
USEFUL FOR

SQL developers, database administrators, and anyone looking to enhance their skills in comparing table data in MSSQL 2012.

WWGD
Science Advisor
Homework Helper
Messages
7,779
Reaction score
13,023
Hi all,
I am trying to find a method to determine if two tables T1, T2 (of any size) in MSSQL2012 are equal.
Please just give hints

My idea is to use the "Except" operator this way:

Select * from T1
Except
(Select * from T2)

And I should get an empty table with columns in both, but with no values entered.
Does this work? Any other hints?
 
Technology news on Phys.org
Only unspecific thoughts, since MSSQL isn't the version I used very often:

Beside automatic PK do you have another one to specify single entries? I can't imagine this will work without 'where' clause plus 'not in'.
Maybe not only PK differ but also timestamps or the spelling of char entries. (Runtime might get a problem, too.)
 
Never mind, Fresh - Prinz_42 of Bel_ Deutschair , I think a correlated subquery using >< should do. Do you agree?
 
Last edited:
WWGD said:
Hi all,
I am trying to find a method to determine if two tables T1, T2 (of any size) in MSSQL2012 are equal.
Please just give hints

My idea is to use the "Except" operator this way:

Select * from T1
Except
(Select * from T2)

And I should get an empty table with columns in both, but with no values entered.
Does this work? Any other hints?
Hints:
  1. Look up the documentation and you will see that that is exactly what EXCEPT is for.
  2. Do you think that you will always get the same results from
    Code:
    SELECT * FROM t2 EXCEPT SELECT * FROM t1
  3. You don't need the ()s.
  4. Look at the documentation to see treatment of schema differences and edge cases.
  5. You could also look at the command line utility tablediff.
  6. Use capitals for reserved words.
Edited: forget to hint about asymmetry
 
  • Like
Likes   Reactions: WWGD
WWGD said:
Never mind, Fresh - Prinz_42 of Bel_ Deutschair , I think a correlated subquery using >< should do. Do you agree?
Depends on how you join them. If you're "lucky" you will select to each A.entry_m all B.entry_n with n≠m. What speaks against select A.entry where A.entry not in (select B.entry where A.entry = B.entry); beside runtime?
 
  • Like
Likes   Reactions: WWGD
WWGD said:
I am trying to find a method to determine if two tables T1, T2 (of any size) in MSSQL2012 are equal.
I tend to do it the hard way. Insert a logical field in T2, set them all to "false" and use a query to set the field to "true" in all matching records. The ones left out are the records where the field value is still "false".
 
  • Like
Likes   Reactions: WWGD
MrAnchovy said:
Hints:
  1. Look up the documentation and you will see that that is exactly what EXCEPT is for.
  2. Do you think that you will always get the same results from
    Code:
    SELECT * FROM t2 EXCEPT SELECT * FROM t1
  3. You don't need the ()s.
  4. Look at the documentation to see treatment of schema differences and edge cases.
  5. You could also look at the command line utility tablediff.
  6. Use capitals for reserved words.
Edited: forget to hint about asymmetry
Thanks, Anchovy, but does your answer assume the tables are normalized? I am thinking I have the same n distinct records in each, but these n records may be
repeated a different number of times in each table, say record_1 appears 7 times in table 1, and appears 8 times in table 2 , etc. How do I account for this; should I select count for each record := record _i and then compare the counts for each?
 
WWGD said:
Thanks, Anchovy, but does your answer assume the tables are normalized? I am thinking I have the same n distinct records in each, but these n records may be
repeated a different number of times in each table, say record_1 appears 7 times in table 1, and appears 8 times in table 2 , etc. How do I account for this; should I select count for each record := record _i and then compare the counts for each?
Sorry for the delay, I haven't been around for a while - but then there was even more of a lag for your question!

It's not going to work with duplicate rows, but then you asked for hints not a complete solution! If there are (or could be) duplicates then yes you are going to need to come up with a different solution. Counting the occurrences for each distinct row in each table and compare the counts could work. Or sort both tables by the same complete ordering, add a sequence column and then use EXCEPT. Or write a script in your language of choice to iterate over sorted recordsets. Or use tablediff. It all depends on the context - why do you want to compare these tables, why might they be different and what do you want to do if they are?
 
  • Like
Likes   Reactions: WWGD
MrAnchovy said:
Sorry for the delay, I haven't been around for a while - but then there was even more of a lag for your question!

It's not going to work with duplicate rows, but then you asked for hints not a complete solution! If there are (or could be) duplicates then yes you are going to need to come up with a different solution. Counting the occurrences for each distinct row in each table and compare the counts could work. Or sort both tables by the same complete ordering, add a sequence column and then use EXCEPT. Or write a script in your language of choice to iterate over sorted recordsets. Or use tablediff. It all depends on the context - why do you want to compare these tables, why might they be different and what do you want to do if they are?
Thanks, it is a theoretical thing, just a challenge someone gave me to help me sharpen my SQL skills. I am allowed to get hints but not full answers.
 
  • #10
I think you can check UNION of both tables and find the rowcount which should equal the rowcount from table T1 as well as table T2. o_O
 
  • Like
Likes   Reactions: WWGD
  • #11
Thanks all, from my reading I think it may be a good idea to use rank or dense rank .

EDIT: I appreciate your suggestions and probably there are a few better than mine but this
is the one I am most familiar with at this point.
 
Last edited:
  • #12
WWGD said:
Thanks, it is a theoretical thing, just a challenge someone gave me to help me sharpen my SQL skills. I am allowed to get hints but not full answers.

To first compare the structures of 2 tables you can use the system view information_schema.columns.:cool:
 
  • Like
Likes   Reactions: WWGD

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 7 ·
Replies
7
Views
4K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
995
  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K