First of all, if this can be done without the use of a cursor, I'm open to switching away from.
The following cursor fetches one by one, names from the iNamesExcel$ sql table. The subsequent dynamic sql looks for matches in Active directory and when found inserts them into a table called temp. There's an asterisk there so that fuzzy matches are also returned.
DECLARE @InventoryNames varchar(100)
DECLARE getInventoryNames CURSOR FOR
FROM getInventoryNames INTO @InventoryNames
WHILE @@FETCH_STATUS = 0
DECLARE @name nvarchar(100)
DECLARE @sql nvarchar(max)
SET @name = @InventoryNames +'*'
INSERT INTO TEMP
SELECT * FROM OPENQUERY (
FROM ''''LDAP://domain name obfuscated''''
objectClass = ''''user''''
and objectCategory = ''''person''''
and displayName = ''''' + @name + ''''''')'
EXEC dbo.sp_executeSQL @sql
FETCH NEXT FROM getInventoryNames
SELECT distinct * FROM TEMP;
Currently the TEMP table only contains the information from AD, but I'd like it to include more columns from the iNamesExcel$ table.
So, I'd like to do a LEFT join to the iNamesExcel$ table, so as to preserve the other columns that are there.
Can I get some ideas on how to do, given the constraint of LDAP Query syntax? Thanks.