We are currently running a script to dump Active Directory user information into a database.
When we do the insert, we hash all the values into a hashcode column of type Varbinary(8000). This column is used to compare the hashes on the next run so that we only update the records that have changed.
At the moment, when the compare is done, it always comes back as different.
I’ve reduce the script to a basic example and still get the same issue.
(Enterprise person is the local table in my database where we keep our people information)
SELECT
SamAccountName
INTO
#TmpEnterprisePerson
FROM OPENQUERY(ADSI, 'SELECT SamAccountName
FROM ''LDAP://MyLdapDirectory''
WHERE SamAccountName = ''myAccount''')
select HashBytes('MD5',COALESCE(EP.SamAccountName, '') ) as originalHash
, HashBytes('MD5',COALESCE(T.SamAccountName, '')) as NewHash
, EP.SamAccountName as originalName
, T.SamAccountName as newName
, '"' + EP.SamAccountName + '"' as originalName2
, '"' + T.SamAccountName + '"' as newName2
, CAST(COALESCE(EP.SamAccountName, '') AS VARbinary(max)) as oriBinaryName
, CAST(COALESCE(T.SamAccountName, '') AS VARbinary(max)) as newBinaryName
, len(ep.samaccountName) as originallength
, len(T.samaccountName) as newLength
FROM
[dbo].[EnterprisePerson] AS EP
INNER JOIN #TmpEnterprisePerson T ON T.SamAccountName = EP.SamAccountName
where ep.SamAccountName= 'myAccount'
drop table #TmpEnterprisePerson
The result is the following
OriginalHash = 0xEB4A732C6372E7F1558D4C95E34CE6FF
NewHash = 0x82DDB9733D5A7532D1C2C734807BE756
OriginalName = MyAccount
NewName = MyAccount //Same as originalName
OriginalNam2 = "MyAccount"
NewName2 = "MyAccount" //Same as originalName2
oriBinaryName= 0x4C415245415541
newBinaryName= 0x4C00410052004500410055004100
OriginalLen = 7
NewLength = 7
I’m not sure what I’m doing wrong when comparing the hashes or why they are coming out differently
I believe you have a datatype issue. Convert
T.SamAccountNameto varchar(N) – same type as what is stored in the database (or cast, if you prefer that over convert).