sql server - SQL Query to loop through AD OUs and get a count of objects -


i'm trying produce report has count of computer objects within many, many ad ous sql server can join other data in sql db , create report ssrs.

i've figured out how single ou (which represents physical site location unique code in situation) need able ous in directory.

this query result 1 single ou. ideas?

            declare @sitecode int             set @sitecode = 1234              declare @region nvarchar(100)             set @region = (                             select top 1        regionname                                        tbl_adsiteinfo                                       sitecode = @sitecode                           )              declare @sitename nvarchar(250)             set @sitename = (                                     select top 1    sitename                                                tbl_adsiteinfo                                               sitecode = @sitecode                               )              declare @sql nvarchar(1000)             set @sql = '             select top 901  count(*) ''count''                        openquery(                                         adsi,                                         ''                                         select distinguishedname,extensionattribute3,extensionattribute4,extensionattribute2, name                                         ''''ldap://ou=computers,ou=' + @sitename + ',ou=' + @region + ',ou=branches,dc=company,dc=com''''                                         objectclass = ''''computer''''                                         ''                                     ) tbladsi              '              exec dbo.sp_executesql @sql 

i figured out how myself after thinking while.

first, query existing table dataset of unique ids in org our sites (sitecode) , write them temp table column called 'processed' has value of 0.

then, while loop has condition of select count(sitecode) #tmp processed = 0 > 1

then select top 1 processed = '0' sitecode , use query table ldap path site's container, , ldap query count of computer objects , write value yet table hold results want. when i've got data need, set record processed = '1' , loop continues.

eventually, sites have processed = '1' , loop finish. drop temp table don't need anymore.

i'm bit concerned execution time blow out doing way apart hand counting, i'm not sure else do, , i'm pretty happy anyway :)


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -