I need to query LDAP with out a linked server. I cannot get my syntax right, between the ' quotes, and the variables I am all mixed up.
SELECT Cast(objectGUID AS uniqueidentifier)
FROM OPENROWSET( 'ADSDSOObject'
,'adsdatasource'; @AdUser ; @adPassword
,'SELECT objectGUID, SAMAccountName
FROM ''LDAP://Domainname.company.com/ou=northamerica,dc=domainname,dc=company,dc=com''
WHERE Name = ' @Lookupname '')
First create a string of the inner SQL query. I would use quote-marks instead of CHAR(39), but note that they should only be doubled-up to one level; It you find it hard to read perhaps create a @Parameter for the double-quote - i.e.
Then I would use that @QuerySql VALUE within the OPENROWSET, also using REPLACE to double-up any quote marks within it. I find this approach much easier than trying to use triple or quadruple quote marks in a single, composite, string. Again, I've used REPLACE on other @Parameters to make sure that any embedded quotes get doubled-up.