• First of all define a Linked Server to the Active Directory Services using the below t-sql command.

    EXEC sp_addlinkedserver 'ADSI', 'Active Directory Services 2.5', 'ADSDSOObject', 'adsdatasource'

    Enable QPENQUERY in SQL Server 2005 and run QPENQUERY T_SQL Statements

    Then try running the below LDAP SELECT queries using t-sql OPENQUERY command.

    Be aware that, Microsoft SQL Server 2005 by default prevents the usage of "OpenQuery", in a way OpenQuery is disabled by default.

    You might have to run the below t-sql script batch in order to enable OpenQuery in a MS SQL Server instance.

    /*

    sp_configure 'show advanced options', 1

    reconfigure with override

    sp_configure 'Ad Hoc Distributed Queries', 1

    reconfigure

    */

    SELECT * FROM OpenQuery(ADSI, 'SELECT * FROM ''LDAP://DC=myserver,DC=com'' WHERE objectCategory=''user'' ')

    SELECT * FROM OpenQuery(ADSI, 'SELECT mail, displayName, userPrincipalName FROM ''LDAP://DC=myserver,DC=com'' WHERE objectCategory=''user'' ')

    You can see that you should use the attribute names properly in the t-sql OPENQUERY statements.