<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Database Programming: The String Concatenation XML Trick, Sans Entitization</title><link>http://blogs.technet.com/wardpond/archive/2008/03/18/database-programming-the-string-concatenation-xml-trick-sans-entitization.aspx</link><description>When last we checked in on The Technique That Lance Found , Adam had noted that the method entitizes XML special characters, a state of affairs which limits its utility somewhat.&amp;#160; I tried to leverage Tony Rogerson's technique , which Adam passed</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Database Programming: The String Concatenation XML Trick, Sans Entitization</title><link>http://blogs.technet.com/wardpond/archive/2008/03/18/database-programming-the-string-concatenation-xml-trick-sans-entitization.aspx#3015166</link><pubDate>Wed, 19 Mar 2008 17:50:56 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3015166</guid><dc:creator>Scott Whigham</dc:creator><description>&lt;p&gt;I love this :) Fun stuff! Here's a CTE version using AdventureWorks - 11ms was my execution time for the CTE - superfast - &lt;/p&gt;
&lt;p&gt;-- List of all Production.ProductSubcategories that a contact has ordered:&lt;/p&gt;
&lt;p&gt;-- Choose contact that has ordered the most items&lt;/p&gt;
&lt;p&gt;DECLARE @ContactId INT&lt;/p&gt;
&lt;p&gt;SELECT @ContactId = ContactId&lt;/p&gt;
&lt;p&gt;FROM Person.Contact &lt;/p&gt;
&lt;p&gt;WHERE ContactId = (SELECT TOP(1) ContactId FROM Sales.SalesOrderHeader GROUP BY ContactId ORDER BY COUNT(*) DESC);&lt;/p&gt;
&lt;p&gt;WITH ColumnToPivot ([data()]) AS (&lt;/p&gt;
&lt;p&gt;		SELECT TOP 100 PERCENT ps.Name + N', ' AS ColumnYouWantToMakeACSVList_PlusAComma&lt;/p&gt;
&lt;p&gt;		FROM Production.ProductSubcategory ps&lt;/p&gt;
&lt;p&gt;		WHERE EXISTS (&lt;/p&gt;
&lt;p&gt;			SELECT *&lt;/p&gt;
&lt;p&gt;			FROM Production.Product p JOIN Sales.SalesOrderDetail od&lt;/p&gt;
&lt;p&gt;				ON od.ProductId = p.ProductId&lt;/p&gt;
&lt;p&gt;			JOIN Sales.SalesOrderHeader o &lt;/p&gt;
&lt;p&gt;			ON o.SalesOrderId = od.SalesOrderId&lt;/p&gt;
&lt;p&gt;			WHERE o.ContactId = @ContactId&lt;/p&gt;
&lt;p&gt;				AND p.ProductSubcategoryID = ps.ProductSubcategoryID&lt;/p&gt;
&lt;p&gt;			)&lt;/p&gt;
&lt;p&gt;		ORDER BY ColumnYouWantToMakeACSVList_PlusAComma -- better to just order by ps.Name but, for example purposes, I include this&lt;/p&gt;
&lt;p&gt;		FOR XML PATH(''), TYPE&lt;/p&gt;
&lt;p&gt;	)&lt;/p&gt;
&lt;p&gt;	, XmlRawData (CSVString) AS (&lt;/p&gt;
&lt;p&gt;		SELECT (SELECT [data()] AS mydata FROM ColumnToPivot AS d FOR XML RAW, TYPE).value( '/row[1]/mydata[1]', 'NVARCHAR(max)') AS CSV_Column&lt;/p&gt;
&lt;p&gt;	)&lt;/p&gt;
&lt;p&gt;SELECT LEFT(CSVString, LEN(CSVString)-1) AS CSVList&lt;/p&gt;
&lt;p&gt;FROM XmlRawData&lt;/p&gt;
&lt;p&gt;I hope the formatting looks okay... I moved the FOR XML clause from a nested derived table to the main CTE - I couldn't find fault with it but perhaps I missed something?&lt;/p&gt;
&lt;p&gt;Thanks for sharing, guys :)&lt;/p&gt;
&lt;p&gt;Scott Whigham&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.learnsqlserver.com/"&gt;http://www.learnsqlserver.com/&lt;/a&gt; &lt;/p&gt;</description></item><item><title>Database Programming: The String Concatenation XML Trick, Finalized</title><link>http://blogs.technet.com/wardpond/archive/2008/03/18/database-programming-the-string-concatenation-xml-trick-sans-entitization.aspx#3017454</link><pubDate>Fri, 21 Mar 2008 17:15:29 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3017454</guid><dc:creator>Ward Pond's SQL Server blog</dc:creator><description>&lt;p&gt;It's an especially Good Friday when we can close the loop on a technical conversation, and I believe&lt;/p&gt;
</description></item><item><title>http://sqlservercode.blogspot.com/2008/03/links-of-week-20080322.html</title><link>http://blogs.technet.com/wardpond/archive/2008/03/18/database-programming-the-string-concatenation-xml-trick-sans-entitization.aspx#3019064</link><pubDate>Mon, 24 Mar 2008 00:28:39 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3019064</guid><dc:creator>TrackBack</dc:creator><description /></item><item><title>May Glad Tidings be Upon You</title><link>http://blogs.technet.com/wardpond/archive/2008/03/18/database-programming-the-string-concatenation-xml-trick-sans-entitization.aspx#3171590</link><pubDate>Sat, 20 Dec 2008 18:55:09 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3171590</guid><dc:creator>Ward Pond's SQL Server blog</dc:creator><description>&lt;p&gt;UPDATED 20 Dec 2008 to fix links It’s that time of year again, when I disappear from the blogosphere&lt;/p&gt;
</description></item><item><title>Database Programming: The String Concatenation XML Trick, Revisited</title><link>http://blogs.technet.com/wardpond/archive/2008/03/18/database-programming-the-string-concatenation-xml-trick-sans-entitization.aspx#3207266</link><pubDate>Fri, 27 Feb 2009 06:31:16 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3207266</guid><dc:creator>Ward Pond's SQL Server blog</dc:creator><description>&lt;p&gt;I’ve got to pay more punctual attention to my comment pool.. RBarryYoung’s movingsql.com will be on my&lt;/p&gt;
</description></item><item><title>T-SQL Challenge: Grouped String Concatenation</title><link>http://blogs.technet.com/wardpond/archive/2008/03/18/database-programming-the-string-concatenation-xml-trick-sans-entitization.aspx#3207487</link><pubDate>Fri, 27 Feb 2009 21:25:05 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3207487</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;It's been quite a while since the LIKE vs ? Puzzle , and I feel like it's time for another one. Response&lt;/p&gt;
</description></item></channel></rss>