January 11, 2011 at 8:11 pm
Comments posted to this topic are about the item Creating a Comma-Separated List (SQL Spackle)
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 12:18 am
Thank you for this tip with the STUFF function.
Is the order by in the subquery really needed? I think the for xml does it anyways. If you don't use the order by; you may use distinct to get every value only once.
WITH CTE AS
(
SELECT DISTINCT
AccountNumber
FROM #TestData
)
SELECT AccountNumber,
CommaList = STUFF((
SELECT distinct ',' + Value --<<-- to get every value only once
FROM #TestData
WHERE AccountNumber = CTE.AccountNumber
--ORDER BY Value --<<--
FOR XML PATH(''),
TYPE).value('.','varchar(max)'),1,1,'')
FROM CTE
ORDER BY AccountNumber;
greetings Reto E.
January 12, 2011 at 3:56 am
reto.eggenberger (1/12/2011)
Thank you for this tip with the STUFF function.Is the order by in the subquery really needed? I think the for xml does it anyways. If you don't use the order by; you may use distinct to get every value only once.
The order by in the subquery is used to control the ordering of the elements in the XML. If you don't use it, then there is no guarantee as to the order. You can use the distinct (or group by) to get each value once. This is independent of the order by clause.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 5:09 am
This is exactly the situation where I use PowerShell frequently. I am able to export data from whatever complex query. Idea is something like this:
Invoke-Sqlcmd -ServerInstance myServer -Database MyDb -Query "SELECT * FROM SomeTable" | Export-Csv ./result.csv -NoTypeInformation
In -Query can be any T-SQL code and results are exported natively to CSV file. I work as ConfigMgr admin and this is the way how you can really easily receive your data.
David
January 12, 2011 at 6:03 am
This is useful. I only have to do this once in a while, and I've been using clumsier methods.
January 12, 2011 at 6:42 am
glock 71629 (1/12/2011)
This is exactly the situation where I use PowerShell frequently. I am able to export data from whatever complex query. Idea is something like this:
Invoke-Sqlcmd -ServerInstance myServer -Database MyDb -Query "SELECT * FROM SomeTable" | Export-Csv ./result.csv -NoTypeInformation
In -Query can be any T-SQL code and results are exported natively to CSV file. I work as ConfigMgr admin and this is the way how you can really easily receive your data.
David
David,
I agree that there are several ways (BCP, PowerShell, etc.) to export the results of a query to a csv file. However, this article is about building a column of comma-separated values as part of a result set... which is completely different.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 7:16 am
Changing
value('.','varchar(max)'),1,1,'')
to
value('(./text())[1]','varchar(max)'),1,1,'')
appears to give a better query plan
Great article BTW.
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537January 12, 2011 at 7:27 am
To all,
Here is an alternative to creating a Comma delimit list directly from a table.
-- Create a Comma Delimited List of Client Numbers (BclCode] in
-- @locBclCodeList from dbo.SomeTable
Declare @locBclCodeList Varchar(Max)
SELECT @locBclCodeList =
COALESCE(LTrim(RTrim(@locBclCodeList)) + ',' ,'') + [SomeColumn]
Fromdbo.SomeTable
-- Joins and Where clauses can go here if you need them
Print ' @locBclCodeList [' + @locBclCodeList + ']'
Rex M Haverty, MCP, DBA
January 12, 2011 at 8:14 am
Wayne, yes! More like this. This is right on target. :smooooth:
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
January 12, 2011 at 8:14 am
Rex - yes, that way will work. However, please see this article[/url] for performance differences between the two methods. String manipulation has never been a strong area for MS, and your method would suffer if there is a lot of data being put into that string.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 8:15 am
Mark-101232 (1/12/2011)
Changingvalue('.','varchar(max)'),1,1,'')
to
value('(./text())[1]','varchar(max)'),1,1,'')
appears to give a better query plan
Thanks for the information... I'll have to test this out.
Edit: WOW! What a difference! Identical queries, with just that one change. The way in the article gets:
(1000 row(s) affected)
Table '#TestData'. Scan count 1001, logical reads 2411, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
SQL Server Execution Times:
CPU time = 234 ms, elapsed time = 3175 ms.
Your suggested change gets:
(1000 row(s) affected)
Table '#TestData'. Scan count 1001, logical reads 2411, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
SQL Server Execution Times:
CPU time = 172 ms, elapsed time = 561 ms.
Looking at the execution plans (both queries run together), the method in the article uses 99% and your solution 1%.
Thanks for the tip!
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 8:48 am
IIRC there was a massive thread here on SSC about how to create comma delimited lists, with lots of code samples and benchmarking. Likewise one to do the reverse - take a delimited list and populate a table from it.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
January 12, 2011 at 9:19 am
Good stuff Wayne.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 12, 2011 at 9:34 am
I've been using something similar to your query for a while now, but I hadn't known about the option to convert to varchar(max) and get rid of the tokens. The tokens have caused me some amount of pain and despite doing a great deal of research, I have never seen your solution anywhere. The point is: THANKS!
January 12, 2011 at 10:10 am
JJ B (1/12/2011)
I've been using something similar to your query for a while now, but I hadn't known about the option to convert to varchar(max) and get rid of the tokens. The tokens have caused me some amount of pain and despite doing a great deal of research, I have never seen your solution anywhere. The point is: THANKS!
JJ - You're welcome. (I have to admit that I picked it up from a post here!)
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 15 posts - 1 through 15 (of 84 total)
You must be logged in to reply to this topic. Login to reply