LDAP查询在Active Directory中查询displayName时使用变量

时间:2013-11-05 23:37:29

标签: sql active-directory

在阅读了这个帖子using openquery to connection to Active directory之后,我找到了一种方法,可以将我的变量替换为LDAP查询进行解析,但它没有返回结果。 (只是列标题)。

declare @name nvarchar(100)
declare @sql nvarchar(max)

set @name = 'Sanjay*'
set @sql=
'SELECT * FROM OPENQUERY (
ADSI,
''SELECT givenname ,
sn,
displayname,
samaccountname,
telephonenumber,
mail,
department,
title
FROM ''''LDAP://domainobfuscated''''
WHERE 
objectClass = ''''USER'''' 
and objectCategory = ''''person''''
and displayName = ''''+@name+'''''')'

exec dbo.sp_executeSQL @sql  

像这样运行它会返回结果:

SELECT * FROM OPENQUERY (
ADSI,
'SELECT givenname ,
    sn,
    displayname,
    samaccountname,
    telephonenumber,
    mail,
    department,
    title

FROM  ''domainobfuscated''

WHERE     
objectClass = ''USER'' 
and objectCategory = ''person''
and givenName = ''Sanjay*''')

0 个答案:

没有答案