2

伙计们!

我正在尝试执行通过 LDAP 请求 ActiveDirectory 文件夹的动态查询。查询如下:

DECLARE @TargetUser VARCHAR(100)
SET @TargetUser = 'apilinktester'

DECLARE @QueryStr VARCHAR(1000), @LDAPStr VARCHAR(100)
SET @LDAPStr = 'LDAP://OU=TechAccounts,DC=some,DC=company,DC=com'
SET @QueryStr = 'SELECT * FROM OpenQuery(ADSI, ''SELECT * FROM ''''' + @LDAPStr + ''''' WHERE objectClass = ''''User'''' AND sAMAccountName = ''''' + @TargetUser + ''''')'
PRINT @QueryStr
EXECUTE (@QueryStr)

我的查询确实失败并出现以下错误:

SELECT * FROM OpenQuery(ADSI, 'SELECT * FROM ''LDAP://OU=TechAccounts,DC=some,DC=company,DC=com'' WHERE objectClass = ''User'' AND sAMAccountName = ''apilinktester'')
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string 'SELECT * FROM 'LDAP://OU=TechAccounts,DC=some,DC=company,DC=com' WHERE objectClass = 'User' AND sAMAccountName = 'apilinktester')'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'SELECT * FROM 'LDAP://OU=TechAccounts,DC=some,DC=company,DC=com' WHERE objectClass = 'User' AND sAMAccountName = 'apilinktester')'. 

有人能帮我吗?

4

1 回答 1

1
DECLARE @TargetUser VARCHAR(100)
SET @TargetUser = 'apilinktester'

DECLARE @QueryStr VARCHAR(1000), @LDAPStr VARCHAR(100)
SET @LDAPStr = 'LDAP://OU=TechAccounts,DC=some,DC=company,DC=com'
SET @QueryStr = 'SELECT * FROM OpenQuery(ADSI, ''SELECT * FROM ''''' 
  + @LDAPStr + ''''' WHERE objectClass = ''''User'''' AND sAMAccountName = ''''' 
  + @TargetUser + ''''''')'
          --need add---^^
PRINT @QueryStr
EXECUTE (@QueryStr)
于 2013-06-17T06:35:25.040 回答