Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to bring in more than one column from left table in a CURSOR/dynamic sql query Expand / Collapse
Author
Message
Posted Wednesday, November 06, 2013 2:11 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, February 17, 2014 10:08 PM
Points: 271, Visits: 728
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.
Post #1512005
Posted Wednesday, November 06, 2013 6:39 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:02 PM
Points: 500, Visits: 2,291
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.



-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1512051
Posted Wednesday, November 06, 2013 7:52 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:42 PM
Points: 3,596, Visits: 5,112
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1512063
Posted Wednesday, November 06, 2013 7:56 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:42 PM
Points: 3,596, Visits: 5,112
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1512064
Posted Monday, November 11, 2013 12:18 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:02 PM
Points: 500, Visits: 2,291
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. 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


-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1513219
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse