SQL Tips by Namwar Rizvi

May 9, 2007

Aggregate concatenation in SQL Server 2005

Filed under: comma seprated list,string concatenation — namwar @ 10:35 PM

In one of my previous posts, I talked about generating a comma separated list in TSQL. One of my readers has pointed out some interesting facts about issues related to this approach.

Following are the issues documented at MSDN

Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression Location

Expression in ORDER BY clause causes self-referencing variables in the SELECT clause to evaluate only once rather than once per row

So what should we do now?
An alternative approach is to use For XML to generate comma separated list. Full working example is available at:

http://milambda.blogspot.com/2005/07/return-related-values-as-array.html
and
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/07/06/871.aspx

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: