November 6, 2013 at 2:11 pm
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
SELECT Name
FROM dbo.iNamesExcel$
OPEN getInventoryNames
FETCH NEXT
FROM getInventoryNames INTO @InventoryNames
WHILE @@FETCH_STATUS = 0
BEGIN
----
DECLARE @name nvarchar(100)
DECLARE @sql nvarchar(max)
SET @name = @InventoryNames +'*'
SET @sql=
'
INSERT INTO TEMP
SELECT * FROM OPENQUERY (
ADSI,
''SELECT displayname,
samaccountname,
telephonenumber,
mail,
department,
title
FROM ''''LDAP://domain name obfuscated''''
WHERE
objectClass = ''''user''''
and objectCategory = ''''person''''
and displayName = ''''' + @name + ''''''')'
EXEC dbo.sp_executeSQL @sql
----
FETCH NEXT FROM getInventoryNames
INTO @InventoryNames
END
CLOSE getInventoryNames
DEALLOCATE 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.
--Quote me
November 6, 2013 at 6:39 pm
You could do something like this (I changed @name to @names):
DECLARE @names varchar(max);
WITH names(n) AS
(
SELECT Name
FROM dbo.iNamesExcel$
FOR XML PATH('')
)
SELECT @names=CHAR(39)+LEFT(n,LEN(n)-1)+CHAR(39)
FROM names
Then, in your DSQL change your and displayName =
to
and displayName IN ( @names )
Hope that makes sense. Let me know if you need clarification.
-- Itzik Ben-Gan 2001
November 6, 2013 at 7:52 pm
Alan.B (11/6/2013)
You could do something like this (I changed @name to @names):
DECLARE @names varchar(max);
WITH names(n) AS
(
SELECT Name
FROM dbo.iNamesExcel$
FOR XML PATH('')
)
SELECT @names=CHAR(39)+LEFT(n,LEN(n)-1)+CHAR(39)
FROM names
Then, in your DSQL change your
and displayName =
to
and displayName IN ( @names )
Hope that makes sense. Let me know if you need clarification.
I don't think that @names is going to be recognized within the scope of the Dynamic SQL.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
November 6, 2013 at 7:56 pm
Something like this might work though:
DECLARE @InventoryNames varchar(100)
DECLARE @sql nvarchar(max)
SET @sql=
'
WITH names(n) AS
(
SELECT Name
FROM dbo.iNamesExcel$
)
INSERT INTO TEMP
SELECT * FROM OPENQUERY (
ADSI,
''SELECT displayname,
samaccountname,
telephonenumber,
mail,
department,
title
FROM ''''LDAP://domain name obfuscated''''
WHERE
objectClass = ''''user''''
and objectCategory = ''''person''''
and displayName IN (SELECT Name FROM names))'
EXEC dbo.sp_executeSQL @sql
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
November 11, 2013 at 12:18 pm
dwain.c (11/6/2013)
Alan.B (11/6/2013)
You could do something like this (I changed @name to @names):
DECLARE @names varchar(max);
WITH names(n) AS
(
SELECT Name
FROM dbo.iNamesExcel$
FOR XML PATH('')
)
SELECT @names=CHAR(39)+LEFT(n,LEN(n)-1)+CHAR(39)
FROM names
Then, in your DSQL change your
and displayName =
to
and displayName IN ( @names )
Hope that makes sense. Let me know if you need clarification.
I don't think that @names is going to be recognized within the scope of the Dynamic SQL.
I was in a hurry and should have tested this my code before posting it:blush:. This is what I was trying to do (I am using sys.databases for a simplified demo that can be tested locally):
WITH dbs(d) AS (SELECT 'tempdb' UNION ALL SELECT 'msdb')
SELECT * INTO #dbs FROM dbs;
DECLARE @names varchar(max),
@sql varchar(1000);
WITH names(n) AS
(
SELECT ''''+d +''','
FROM #dbs
FOR XML PATH('')
)
SELECT @names=LEFT(n,LEN(n)-2)
FROM names;
SET @sql=
'SELECT * FROM sys.databases
WHERE name IN (' + @names + ''')';
EXEC (@sql);
That said, the solution you posted was much better:-P
-- Itzik Ben-Gan 2001
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply