Nifty Ways to Use FOR XML PATH Concatenation

Use it to list all columns in an index or all files in a file group

Downloads
99197.zip

Executive Summary: No temporary tables or table variables are needed to perform FOR XML PATH concatenation, so the resulting query is not only fast but also high performing.

In Inside Microsoft SQL Server 2005: T-SQL Programming (Microsoft Press, 2006) by Itzik Ben-Gan, Dejan Sarka, and Roger Wolter, I was delighted to read about a clever way of using SQL Server 2005’s FOR XML PATH option to concatenate string values in rows into a single column or result set without using temporary tables or table variables (see page 109). This unorthodox method of concatenating string values can be quite useful in queries. For example, you can use it to list all columns in an index or all files in a file group.

See also: How do I retrieve values from a table and store the results in a string?

Listing 1 shows a traditional query to list all the columns in the indexes for a table or view. In the results in Figure 1, notice that the index IX_Address_AddressLine1_Address Line2_City_StateProvince_PostalCode_Country Region (index_id 3) has six columns: AddressLine1, AddressLine2, City, StateProvince, PostalCode, and CountryRegion.

You can use the FOR XML PATH option and the text() XPath node test (see SQL Server 2005 Books Online—BOL at msdn.microsoft.com/en-us/library/ms345115.aspx) to list all the indexes’ columns in a single concatenated string, as shown in Listing 2. Figure 2 shows the results from this query. As you can see, a comma is prepended to each column name. Granted, having all the column names listed together like this is of limited use. However, you can refine this technique so that it produces useable results. By using the handy STUFF string function with the FOR XML PATH option, you can get output like that in Figure 3, page 10.

In Figure 3, notice the row for the multi-column index IX_Address_AddressLine1_AddressLine2_City_StateProvince_PostalCode_CountryRegion. The AddressLine1, AddressLine2, City, StateProvince, PostalCode, and CountryRegion columns are all now in this row, with their names separated by a comma. Listing 3, page 10, shows the query that was used to produce this result. In this query, the STUFF function removes the comma preceding the first name in the comma-separated column list.

The query in Listing 3 is only meant to demonstrate how you can concatenate the names of columns inmulticolumn indexes. A query that you can use on your databases to list all the columns in every index is available online in the file named FullQuery.sql. You can download this file by going to www.sqlmag.com, entering 99197 in the InstantDoc ID text box, clicking Go, then clicking the 99197.zip hotlink.

You can use the FOR XML PATH option with the STUFF function for other tasks. For example, you can use this technique to concatenate the names of files in a file group, as shown in Listing 4, page 10. In addition to the FOR XML PATH option and STUFF function, this query uses a derived table and the dynamic management view (DMV) dm_io_virtual_file_stats to summarize statistics for all the databases and all the files given that NULL, NULL parameters are passed to dm_io_virtual_file_stats. The DMV provides detailed information for each file in the database. The query in Listing 4 groups the results by database, as Figure 4 shows. This can be handy for looking at databases from a high-level perspective so that, for example, you can easily determine which database has the highest reads. You’d just need to sort the results by descending DB_Reads.

Note that no temporary tables or table variables were needed to perform these nifty concatenation operations. As Inside Microsoft SQL Server 2005: T-SQL Programming mentions, not only is the resulting query “short and slick” but it is also high performing. With these examples as a starting point, can you think of other creative ways to use FOR XML PATH to display concatenated string results?

See also: UDF Eliminates Concatenation Woes

—Raul Pubill, senior database administrator, JP Morgan FCS

Discuss this Article 1

dschaeff
on Jul 10, 2008
Very useful. FullQuery could be enhanced by excluding included columns by adding the restriction "AND ic.is_included_column = 0" just before the line "ORDER BY ic.key_ordinal". Add another column to the select list and another derived table to separately see the list of included columns.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
Vision road sign
May 6, 2013
blog

Cheaters Never Win, Even in TPC Benchmarks

In this portion of the series on database benchmarking, I want to tell you about one of my favorite aspects of the TPC benchmarks – CHEATING....More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.