• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

Record bit comparison

Is there a way to compare two records to determine if any of the fields contain different values WITHOUT having to compare each and every field? Something like a bit comparison between each record?

I need to compare employee data from a thrid party program which is downloaded each week against the current data. I need to determine if data in any field is different and if so write the old record to the archive table and insert the 3rd party record to the current table.  There are about 100 fields and I would love to be able to compare it without having to write out every field.

Thanks - Charlie
0
CLoprinzo
Asked:
CLoprinzo
  • 3
  • 2
1 Solution
 
mohan_sekarCommented:
How refined should it be? Say, you've John Smith in your table and JohnSmith in the 3rd party table. How do you want to deal that?
0
 
chapmandewCommented:
you can do it...sure.  you can do something like this (you'll need to add a computed column)

alter table tablename
add bitmask as binary_checksum(field1, field2, field3, ...)

then, you can just run sql queries to compare the new bitmask field to the bitmask of other records to tell if they are the same.  There is a possibility of collisions (diferrent values hashing to be the same value over a set of fields), but it is unlikely.
0
 
CLoprinzoAuthor Commented:
Basically I need to determine if any field has changed (Employment Status, Marital Status, pay rate, address, etc.) and if so I want to insert the record into tblEmployeeDetail _Archive and delete it from the tblEmployeeDetail , then insert the downloaded one into tblEmployeeDetail.

chapmandew - I am not familiar with binary_checksum... it will only work on altered records in a table correct? or am I reading it wrong?  I need to compare record ABC from tblEmployeeDetail to ABC from
the downloaded table.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
chapmandewCommented:
add the computed column to both tables (with same defintion), and compare the records

--this will give you the records that have been updated...
select * from tbl_employeedetail d join tbl_employeedetail_archive a
on d.primarykey = a.primarykey
where a.bitmask <> d.bitmask
0
 
CLoprinzoAuthor Commented:
Your rank says it all ... Genius!

That worked exactly the way I wanted and it's pretty fast too!!  Thanks a million!!
0
 
chapmandewCommented:
Well I really appreciate that.  Glad to help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now