﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / In The Enterprise  / SQL ADSI Query limitation / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 10:12:44 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>Definite 255 char limit. See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65584I used the function to get around and loop throughdeclare @Apptemp3 table (BSamName varchar(255),						BDisplayName varchar(255),						BPhone varchar(100),						BEMail varchar(255),						BFirstName varchar(100),						BLastName varchar(100))DECLARE @sChar char(1)declare @body varchar(8000)DECLARE @nAsciiValue smallintSELECT @nAsciiValue = 65WHILE @nAsciiValue &amp;lt; 91 BEGIN  SELECT @sChar=  CHAR(@nAsciiValue)SET @body = (select dbo.fnSprintf('SELECT sAMAccountName, displayName, mail, telephoneNumber, sn, givenName FROM OPENQUERY( ADSI, ''SELECT displayName, sAMAccountName, mail, telephoneNumber, sn, givenName  FROM ''''LDAP://dc=cchcs,dc=ldap''''WHERE objectCategory = ''''Person'''' AND SAMAccountName = ''''%s*'''''' )', @SChar +'*', default))         INSERT @Apptemp3  EXEC( @body)    SELECT @nAsciiValue = @nAsciiValue + 1 ENDSelect ISNULL(Upper(BSamName),'NoSamID'),ISNULL(BDisplayName,'NoDName'),ISNULL(BPhone,'NoPhone'),ISNULL(BEMail,'Nomail'),ISNULL(BFirstName,'NoFname'),ISNULL(BLastName,'NoLname') from @Apptemp3order by BSamName</description><pubDate>Thu, 09 Feb 2012 14:54:30 GMT</pubDate><dc:creator>Brian ODwyer</dc:creator></item><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>I found that to be true as well, think it is something to do with the string length, used * to just pull all the columns and save on the string length instead of directly naming then in the select. Tried increasing the string length from 255 to 512 and it did not work. Will research it.</description><pubDate>Thu, 09 Feb 2012 12:36:25 GMT</pubDate><dc:creator>Brian ODwyer</dc:creator></item><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>[quote][b]Jan-155192 (10/25/2005)[/b][hr]By the way, I ended up with the following code. Thanks for helping.JanCREATE TABLE #tmpADUsers( employeeIdvarchar(10) NULL,SAMAccountNamevarchar(255) NOT NULL,emailvarchar(255) NULL)GO/* AD is limited to send 1000 records in one batch. In an ADO interface you can define this batch size, not in OPENQUERY.Because of this limitation, we just loop through the alphabet.*/DECLARE @cmdstr varchar(255)DECLARE @nAsciiValue smallintDECLARE @sChar char(1)SELECT @nAsciiValue = 65WHILE @nAsciiValue &amp;lt; 91BEGINSELECT @sChar= CHAR(@nAsciiValue)EXEC master..xp_sprintf @cmdstr OUTPUT, 'SELECT employeeId, SAMAccountName, Mail FROM OPENQUERY( ADSI, ''SELECT Mail, SAMAccountName, employeeID FROM ''''LDAP://dc=central,dc=mydomain,dc=int''''WHERE objectCategory = ''''Person'''' AND SAMAccountName = ''''%s*'''''' )', @sCharINSERT #tmpADUsersEXEC( @cmdstr )SELECT @nAsciiValue = @nAsciiValue + 1ENDDROP TABLE #tmpADUsers[/quote]I know this reply is over a year later but I came across your post while searching on the ADSI Linked server provider.  I can get your sample to work but I hit a brick wall when I try to get too many columns of data and I can't see where you change this if at all.  I'm trying to access 5 more items in addition to the 3 in your code and I can access all of them if I select only part of the 8 fields at once.  For example of the 8 I'm trying to get: employeeID, SAMAccountName, Mail, DisplayName, sn, givenName, department &amp; userAccountControlI can get any 5-6 but if I try for 6 to 8, depending on the combination I get an error.Thoughts?  </description><pubDate>Thu, 19 May 2011 13:29:19 GMT</pubDate><dc:creator>YSLGuru</dc:creator></item><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>SSC-EnthusiasticYou are the best. I have been trying to come up with something to overcome the 1000 limit on and off for the last few weeks.Brilliant solution and thanks to you.</description><pubDate>Thu, 10 Dec 2009 02:32:08 GMT</pubDate><dc:creator>jonathan.stokes</dc:creator></item><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>&lt;P&gt;Jan - I misunderstood your original question. Kudos on the work-around and thank you for posting it on the forum.&lt;/P&gt;&lt;P&gt; I did, however,find a Microsoft article (Article 299410) that specifically addresses the paging issue within a distributed query. You must use other means to retrieve more than 1000 objects from ADSI. Quote:&lt;/P&gt;&lt;P&gt;&lt;FONT color=#113311&gt;"While paging is available through ADSI's OLEDB provider, &lt;STRONG&gt;&lt;U&gt;there is currently no way available to perform it from a SQL distributed query&lt;/U&gt;&lt;/STRONG&gt;. This means that the total number of objects that can be returned for a query is the server limit. In the Windows 2000 Active Directory, the default server limit is 1,000 objects."&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.microsoft.com/default.aspx?scid=kb;en-us;299410"&gt;http://support.microsoft.com/default.aspx?scid=kb;en-us;299410&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Don&lt;/P&gt;</description><pubDate>Tue, 25 Oct 2005 06:08:00 GMT</pubDate><dc:creator>Don Staten</dc:creator></item><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>&lt;P&gt;By the way, I ended up with the following code. Thanks for helping.&lt;/P&gt;&lt;P&gt;Jan&lt;/P&gt;&lt;P&gt;CREATE TABLE #tmpADUsers (  employeeId varchar(10) NULL,  SAMAccountName varchar(255) NOT NULL,  email  varchar(255) NULL)GO&lt;/P&gt;&lt;P&gt;/* AD is limited to send 1000 records in one batch. In an ADO interface you can define this batch size, not in OPENQUERY.Because of this limitation, we just loop through the alphabet.*/&lt;/P&gt;&lt;P&gt;DECLARE @cmdstr varchar(255)DECLARE @nAsciiValue smallintDECLARE @sChar char(1)&lt;/P&gt;&lt;P&gt;SELECT @nAsciiValue = 65&lt;/P&gt;&lt;P&gt;WHILE @nAsciiValue &amp;lt; 91 BEGIN&lt;/P&gt;&lt;P&gt;  SELECT @sChar=  CHAR(@nAsciiValue)&lt;/P&gt;&lt;P&gt;  EXEC master..xp_sprintf @cmdstr OUTPUT, 'SELECT employeeId, SAMAccountName, Mail FROM OPENQUERY( ADSI, ''SELECT Mail, SAMAccountName, employeeID FROM ''''LDAP://dc=central,dc=mydomain,dc=int''''WHERE objectCategory = ''''Person'''' AND SAMAccountName = ''''%s*'''''' )', @sChar          INSERT #tmpADUsers  EXEC( @cmdstr )    SELECT @nAsciiValue = @nAsciiValue + 1 END&lt;/P&gt;&lt;P&gt;DROP TABLE #tmpADUsers&lt;/P&gt;</description><pubDate>Tue, 25 Oct 2005 04:19:00 GMT</pubDate><dc:creator>Jan-155192</dc:creator></item><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>&lt;P&gt;Vinny &amp;amp; Newbie,&lt;/P&gt;&lt;P&gt;Thanks, but I knew this. My question is: how can we do this paging from the OPENQUERY function?&lt;/P&gt;&lt;P&gt;In a vb script (or for that matter any language that uses ADO), we can write something like that:&lt;/P&gt;&lt;P&gt; objCommand.CommandText = _ "&amp;lt;&lt;A href="ldap://dc=Central,dc=mydomain,dc=int&gt;;(objectCategory=user"&gt;LDAP://dc=Central,dc=mydomain,dc=int&amp;gt;;(objectCategory=user&lt;/A&gt;)" &amp;amp; _ ";distinguishedName,Name,sAMAccountName,primaryGroupID,memberOf,canonicalName,Mail,telephoneNumber;subtree"&lt;FONT color=#ff1111&gt;&lt;STRONG&gt;objCommand.Properties("Page Size") = 50&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#111111&gt;This tells AD to send batches with 50 records at one time. But where can I put this parameter in the SQL syntax? The following doesn't work:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;SELECT employeeId, SAMAccountName, Mail  FROM OPENQUERY( ADSI, 'SELECT Mail, SAMAccountName, employeeID FROM 'LDAP://dc=central,dc=mydomain,dc=int''WHERE objectCategory = ''Person'';&lt;FONT color=#ff1111&gt;''Page Size''=50&lt;/FONT&gt;' ) &lt;/P&gt;&lt;P&gt;I can partition (not page!) the query like this:&lt;/P&gt;&lt;P&gt;... FROM OPENQUERY( ADSI, 'SELECT Mail, SAMAccountName, employeeID FROM ''LDAP://dc=central,dc=mydomain,dc=int''WHERE objectCategory = ''Person'' AND &lt;FONT color=#ff1111&gt;SAMAccountName = ''A*''&lt;/FONT&gt;' )&lt;/P&gt;&lt;P&gt;and ask for A,B,C, ... , Z. But I prefer the paging attribute, and would like to know how to submit it.&lt;/P&gt;&lt;P&gt;Many thanks,&lt;/P&gt;&lt;P&gt;Jan&lt;/P&gt;</description><pubDate>Tue, 25 Oct 2005 01:47:00 GMT</pubDate><dc:creator>Jan-155192</dc:creator></item><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>&lt;P&gt;Vinny is right. ADSI is limited to returning the first 1000 records without paging. Here's a Microsoft article for reference:&lt;/P&gt;&lt;P&gt;&lt;A href="http://msdn.microsoft.com/library/en-us/adsi/adsi/paging_with_idirectorysearch.asp?frame=true"&gt;http://msdn.microsoft.com/library/en-us/adsi/adsi/paging_with_idirectorysearch.asp?frame=true&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Quote from article:&lt;/P&gt;&lt;P&gt;&lt;FONT color=#113311&gt;"Searches of the Active Directory performed without paging are limited to returning a maximum of the first 1000 records, so you must use a paged search if there is the possibility that the result set will contain more than 1000 items."&lt;/FONT&gt;&lt;/P&gt;</description><pubDate>Mon, 24 Oct 2005 12:39:00 GMT</pubDate><dc:creator>Don Staten</dc:creator></item><item><title>RE: SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>&lt;P&gt;Have a search on the site, as someone else asked the same question a week or so ago. Basically though, my understanding from the answers to that article was that the limit of 1000 records is set (hardcoded!) in AD and there is nothing you can do to change it ... therefore, paging your results was the only way to accomplish what you want!&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Mon, 24 Oct 2005 10:35:00 GMT</pubDate><dc:creator>vinny_1973</dc:creator></item><item><title>SQL ADSI Query limitation</title><link>http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx</link><description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to query a list of 2500 users out of the AD in SQL Server with the following query:&lt;/P&gt;&lt;P&gt;SELECT employeeId, SAMAccountName, Mail  FROM OPENQUERY( ADSI, 'SELECT Mail, SAMAccountName, employeeID FROM 'LDAP://dc=central,dc=mydomain,dc=int''WHERE objectCategory = ''Person''' )&lt;/P&gt;&lt;P&gt;However the result set seems to be limited to 1000 records. I have read that the "Page Size" parameter for AD should be given, but I don't know the exact SQL syntax.&lt;/P&gt;&lt;P&gt;... 'LDAP://dc=central,dc=mydomain,dc=int''WHERE objectCategory = ''Person'';''Page Size''=50' ) doesn't work. Neither does the range parameter.&lt;/P&gt;&lt;P&gt;Somebody suggested that I should partition the query, asking first for all userids starting with A, then B,... but I don't like that suggestion too much &lt;img src='images/emotions/sick.gif' height='20' width='20' border='0' title='Sick' align='absmiddle'&gt; and even if I would use it, the Like 'A%' doesn't work.&lt;/P&gt;&lt;P&gt;Does somebody have any experience with this?&lt;/P&gt;&lt;P&gt;Many thanks,&lt;/P&gt;&lt;P&gt;Jan&lt;/P&gt;</description><pubDate>Mon, 24 Oct 2005 08:14:00 GMT</pubDate><dc:creator>Jan-155192</dc:creator></item></channel></rss>