0

我们目前正在运行一个脚本来将 Active Directory 用户信息转储到数据库中。

当我们进行插入时,我们将所有值散列到 Varbinary(8000) 类型的散列码列中。此列用于比较下次运行时的哈希值,以便我们只更新已更改的记录。目前,当比较完成时,它总是以不同的方式返回。

我已将脚本简化为一个基本示例,但仍然遇到同样的问题。(企业人员是我数据库中保存人员信息的本地表)

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

结果如下

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

我不确定在比较哈希时我做错了什么或为什么它们的结果不同

4

1 回答 1

1

我相信您有数据类型问题。转换T.SamAccountName为 varchar(N) - 与存储在数据库中的类型相同(或转换,如果您更喜欢转换)。

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', convert(varchar(50), COALESCE(T.SamAccountName), ''))) as NewHash
        , EP.SamAccountName as originalName
        , T.SamAccountName as newName
        , 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
于 2012-12-06T13:25:03.823 回答