Solved

Querying AD

Posted on 2006-06-25
5
208 Views
Last Modified: 2012-06-27
Hi,
I have some code that is VB and is used to connect to active directory using LDAP
strSql = "Select sn, givenName, name"
strSql = strSql & "From 'LDAP://DC = Local,DC=OurPlace,DC= govt, DC=NZ' " 'rob
strSql = strSql & "WHERE objectCategory='Person' "
strSql = strSql & "AND objectClass = 'User' "

Is it possible to have this inside a stored procedure?
I tried in QA but it spews on the "FROM" line.
0
Comment
Question by:QPR
  • 3
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 500 total points
Comment Utility
OPENROWSET should do it.

To initially get it working you need to ditch that dynamic SQL and use query analyzer directly to test it.


I hate to be just a 'link poster' but this looks like what you're after:


http://www.dbtalk.net/microsoft-public-sqlserver-clients/ldap-openrowset-150001.html
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
Thanks, I'll check the link.
I did cut the string stuff in QA but I kept getting syntax errors for...

From LDAP://DC = Local,DC=OurPlace,DC= govt, DC=NZ
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
That almost works great.
If I just select name from etc I get that column.
Not knowing the other column names I tried a select * but got a ton of cryptic non-SQL errors.
Any idea how to retrieve the column names?
If not, I'll get the network admin to tell me.
0
 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
FYI when a name has spaces or special characters you surround it with square brackets.

Like this:


From [LDAP://DC = Local,DC=OurPlace,DC= govt, DC=NZ]


That particular thing won't actually work, but if you ever have a strangely named table or field, just surround it with square brackets.


For PAQ reference in case the above link goes down, here is the syntax:




SELECT distinguishedName, name
FROM OPENROWSET('ADSDSOObject', 'adsdatasource'
, 'SELECT distinguishedName, name, memberOf
FROM ''LDAP://microsoft.com/DC=microsoft,DC=com''
WHERE objectClass = ''user''
'
)



The final link on that page:

http://msdn.microsoft.com/library/de...ce_mapping.asp

indicates that these are all valid field names:


givenname
sn
initials
displayname
description
mail

userprincipalname
samaccountname

memberof
primarygroupid

profilepath
scriptpath

department
company
manager



There are a couple more on that particular link.


You may have to use a process of elimination if some of them won't return.
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
thanks, stupidly I wasn't reading correctly and was trying to select fields from the outer select that weren't "selected in the inner if you get my drift. The network admin has shown me the AD schema so I now know which columns to selct.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now