SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to bring in more than one column from left table in a CURSOR/dynamic sql query


How to bring in more than one column from left table in a CURSOR/dynamic sql query

Author
Message
polkadot
polkadot
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5694 Visits: 1257
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
Alan Burstein
Alan Burstein
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24278 Visits: 8349
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


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

dwain.c
dwain.c
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32161 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dwain.c
dwain.c
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32161 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Alan Burstein
Alan Burstein
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24278 Visits: 8349
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 itBlush. 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

-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search