I’m wondering if the current process I’m using to update a table of user’s (tblUsers) Windows ID’s (NTID) is a good method. I’m wondering because LDAP will only return 1000 rows I believe, so that prevents me from just doing it all in one query.
tlbUsers has about 160,000 rows. I’m querying LDAP to update the NTID of each record in tblUsers. I’m using a linked server to ADSI to view LDAP data. My process uses two stored procedures, one for getting a WindowsID from LDAP (LdapPackage.GetUserNTID), another for updating the rows in tblUsers (LdapPackage.UpdateUserNTID).
The code below works for updating the table, however, it’s pretty slow. It would seem to me this isn’t the best way of doing it, that if I wanted to do a batch update like this from LDAP, there should be a simpler way than updating a record at a time.
This previous post gave an interesting example using UNION’s to get around the 1000 record limit, but it only works if each query returns less than 1000 records, which at a large company would probably require lots of UNIONS… at least that’s my initial take on it.
Querying Active Directory from SQL Server 2005
Thanks in advance guys!!!
<code>
CREATE PROCEDURE LdapPackage.GetUserNTID
(
@EmployeeID INT,
@OutNTID VARCHAR(20) OUTPUT
)
AS
BEGIN
DECLARE @SQLString NVARCHAR(MAX)
DECLARE @ParmDefinition NVARCHAR(MAX)
DECLARE @LdapFilter NVARCHAR(100)
--DECLARE @NTID VARCHAR(20)
SET @LdapFilter = 'employeeNumber = ' + CAST(@EmployeeID AS NVARCHAR(20))
SET @SQLString = 'SELECT DISTINCT @pNTID = samAccountName
FROM OPENQUERY(LDAP,
''select samAccountName, Mail
from ''''GC://domain.company.com''''
where objectClass=''''user'''' AND objectCategory=''''person'''' and ' + @LdapFilter + ''')
WHERE Mail IS NOT NULL'
SET @ParmDefinition = N'@pNTID varchar(20) OUTPUT'
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@pNTID=@OutNTID OUTPUT
--SELECT NTID = @OutNTID
END
</code>
<code>
CREATE PROCEDURE LdapPackage.UpdateUserNTID
AS
BEGIN
DECLARE @EmployeeID AS INT
DECLARE @NTID AS VARCHAR(20)
DECLARE @RowCount AS INT
DECLARE @SQLString AS NVARCHAR(MAX)
DECLARE @ParmDefinition AS NVARCHAR(200)
SET @RowCount = 1
DECLARE Persons CURSOR
FOR SELECT DISTINCT EmployeeID FROM tblUsers
OPEN Persons
FETCH NEXT FROM Persons INTO @EmployeeID
WHILE @@FETCH_STATUS = 0
BEGIN
--GET NTID
SET @SQLString =N'EXEC LdapPackage.GetUserNTID @pEmployeeID, @pNTID OUTPUT'
SET @ParmDefinition =N'@pEmployeeID INT, @pNTID VARCHAR(20) OUTPUT'
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@pEmployeeID=@EmployeeID,
@pNTID=@NTID OUTPUT
--UPDATE NTID
/*PRINT 'RowCount = ' + CAST(@RowCount AS VARCHAR(10))
PRINT 'EmployeeID = ' + CAST(@EmployeeID AS VARCHAR(20))
PRINT 'NTID = ' + @NTID
PRINT '-----------------------------'*/
UPDATE tblUsers
SET NTID = @NTID
WHERE EmployeeID = @EmployeeID
SET @RowCount = @RowCount + 1
FETCH NEXT FROM Persons INTO @EmployeeID
END
CLOSE Persons
DEALLOCATE Persons
END
</code>
my solution here was to have my that linked servers record limit to LDAP increased by the system admin. I would have preferred to have identified some sort of SQL Server interface like Oracle appears to have… so maybe I’ll get to that in the future.