在使用 openquery 连接到 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*''')