【问题标题】:using openquery to connection to Active directory使用 openquery 连接到 Active Directory
【发布时间】:2012-08-29 18:52:48
【问题描述】:

我有以下代码连接到我的活动目录,然后将用户拉到某个组中。由于代码在下面,它工作正常,但是我必须硬编码我想要查看的部门。

我正在尝试将参数传递给 openqueries 第二个参数(第二个代码),但我一直收到错误,我知道这是我的引号有问题,非常感谢任何帮助,谢谢,

select *
from openquery(ADSI, '

select
givenName, 
sn, 
sAMAccountName, 
displayName,
mail, 
telephoneNumber, 
mobile, 
physicalDeliveryOfficeName, 
department, 
division


from ''LDAP://DC=directorysync,DC=cfc, DC=com'' 
where objectCategory = ''Person'' 
and objectClass = ''user''
and department = ''Production''
AND displayname <> ''0_UW_Template_Remote''
ORDER BY displayName
')



select *
from openquery(ADSI, '

select
givenName, 
sn, 
sAMAccountName, 
displayName,
mail, 
telephoneNumber, 
mobile, 
physicalDeliveryOfficeName, 
department, 
division


from ''LDAP://DC=directorysync,DC=cfc, DC=com'' 
where objectCategory = ''Person'' 
and objectClass = ''user''
and department = '''+@Department+'''
AND displayname <> ''0_UW_Template_Remote''
ORDER BY displayName
')

【问题讨论】:

    标签: sql-server active-directory


    【解决方案1】:

    您不能在 openquery 调用中构造查询,您需要在变量中构建查询然后执行它。

    declare @qry varchar(8000)
    set @qry = 'select *
    from openquery(ADSI, ''
        select
        givenName, 
        sn, 
        sAMAccountName, 
        displayName,
        mail, 
        telephoneNumber, 
        mobile, 
        physicalDeliveryOfficeName, 
        department, 
        division
    
    
        from ''''LDAP://DC=directorysync,DC=cfc, DC=com'''' 
        where objectCategory = ''''Person'''' 
        and objectClass = ''''user''''
        and department = '''''+@Department+'''''
        AND displayname <> ''''0_UW_Template_Remote''''
        ORDER BY displayName
    '')'
    
    exec(@qry)
    

    【讨论】:

    • 抱歉,我无法测试它,您需要将整个查询构建到变量中。答案已更新。
    猜你喜欢
    • 1970-01-01
    • 2012-01-23
    • 2014-01-06
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    相关资源
    最近更新 更多