Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL statement to use more than 1 OU in LDAP query

Status
Not open for further replies.

58sniper

MIS
Apr 25, 2004
9,152
US
I am using the following to search LDAP to compile a web based phone book:

Code:
	sQuery = "SELECT name, telephonenumber, mail, department, title, displayname, sAMAccountname "
	sQuery = sQuery & "FROM 'LDAP://main-dc2/ou=Users,OU=Troy Campus,dc=mydomain,dc=org' "
	sQuery = sQuery & "WHERE objectCategory='person' AND objectClass='User' "
	' sQuery = sQuery & "WHERE objectClass='User' "
	' If (Len(sDepartment) > 0) Then sQuery = sQuery & "AND department='" & sDepartment & "' "
	If (Len(sSortBy) > 0) Then
		sQuery = sQuery & "ORDER BY " & sSortBy
	Else
		sQuery = sQuery & "ORDER BY name"
	End if

However, I need to search 4 OUs. I can't for the life of me figure out how to construct the query statement to look in all four OUs. I can change it to

Code:
	sQuery = sQuery & "FROM 'LDAP://dc=mydomain,dc=org' "

But that queries all of AD, yielding results I don't want. There is no common OU that I can use. How do I specify 4 OUs in the SQL statement? I've looked around and can't seem to find something that works.

Pat Richard, MCSE MCSA:Messaging CNA
Microsoft Exchange MVP
 

I dont know what an OU is or LDAP so excuse me if this is complete hogwash but in SQL terms it looks like you want a union e.g

SELECT name, telephonenumber, mail, department, title, displayname, sAMAccountname
FROM 'put your 1st LDAP thingy in here'
UNION
SELECT name, telephonenumber, mail, department, title, displayname, sAMAccountname
FROM 'put your 2nd LDAP thingy in here'
UNION etc....

 
taupirho,
Sorry this is not the same thing...this is not a query to a SQL/Access Database but to Active Directory.

58sniper,
I don't think I've seen or come across (though I could be wrong) of a way to search only certain OU's all at once. You may simply have to do 4 seperate searches for what you need.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
dm4ever

"You may simply have to do 4 seperate searches for what you need."

I kind of thought that was what would be rquired and that is what my post was about. i.e
don't do 4 searches followed by 4 sets of processing of search results. Do 1 search - albeit a union - and 1 processing of search results.


 
What about storing the output of each query into an array and then using the 4guysfromrolla's vbscript sort example to sort the output?

Code:
<script language=JScript runat=server>
    function SortVBArray(arrVBArray) {
        return arrVBArray.toArray().sort().join('\b');
    }
</script>

    Function SortArray(arrInput)
        SortArray = Split(SortVBArray(arrInput), Chr(8))
    End Function


Thanks

John Fuhrman
Titan Global Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top