|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 5,243,
Visits: 7,055
|
|
BowlOfCereal (9/28/2012) There's been a lot of discussion in this (oddly) revived thread today. The reason for the revival of the thread is that the article was prominently featured in the newsletter.
According to Microsoft's KB article and Connect comments: the unpredictable, unsupported behavior occurs "when you apply any operators or expressions to the ORDER BY clause of aggregate concatenation queries". I understand that (or at least I think I do). But this has nothing to do with building a comma delimited string. The original article said nothing about applying operators to an ORDER BY clause.
So, my question: is it true that the method presented in this article is "known to potentially return incorrect results"? From the relevant Microsoft Knowledge Base article: "The correct behavior for an aggregate concatenation query is undefined." If the correct behaviour is undefined, then it's impopssible to tell if a given result is incorrect. Ergo, the method presented in this article can by definition never return "incorrect" results.
However, the results returned may be completely different than what you expect, or want. For instance, when the optimizer chooses to use a parallel plan, it could just return the result from one of the threads, which you would probably consider incorrect. I don't think that the optimizer will at this time choose a parallel plan for queries of this type (I just spent a half hour trying very hard to get it to, but failed) - but since this is undocumented behaviour of the optimizer, that might change. And if a future change to the optimizer causes it to create a parallel plan for this query, your bug reports will probably be closed as "by design" - since "the correct behavior for an aggregate concatenation query is undefined."
For me, this is enough reason to avoid this method. The XML method is a perfect replacement - and this method IS documented, and hence guaranteed.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 2:50 PM
Points: 57,
Visits: 317
|
|
Hugo Kornelis (9/28/2012)
However, the results returned may be completely different than what you expect, or want. For instance, when the optimizer chooses to use a parallel plan, it could just return the result from one of the threads, which you would probably consider incorrect. I don't think that the optimizer will at this time choose a parallel plan for queries of this type (I just spent a half hour trying very hard to get it to, but failed) - but since this is undocumented behaviour of the optimizer, that might change. And if a future change to the optimizer causes it to create a parallel plan for this query, your bug reports will probably be closed as "by design" - since "the correct behavior for an aggregate concatenation query is undefined."
For me, this is enough reason to avoid this method. The XML method is a perfect replacement - and this method IS documented, and hence guaranteed.
Thanks for the reply; these are excellent points. I still read the KB article as specifically having to do with the ORDER BY issue, but I'm coming around to seeing your larger point. Today, the ORDER BY issue may be the only way to expose the problem with the "aggregate concatenation query" approach, but there's no guarantee that will be true tomorrow.
And as you point out, given that there's a perfectly sound alternative, we'd all certainly be wise to explore the XML method. That will improve my future code; whether I get time to refactor my existing stuff is another matter.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Saturday, September 29, 2012 6:38 AM
Points: 276,
Visits: 21
|
|
Hello.
I'm used this one, without cursor:
declare @sep varchar(1) declare @text varchar(max) set @sep=''
select @text=@text + @sep + column1, @sep=',' from table
Modification for distinct version was shown: replace table by (select distinct column1 from table) as t
regards Jarek
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 2:59 PM
Points: 11,
Visits: 71
|
|
| I just want to say thanks, I can use this to get rid of several while loops.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, February 07, 2013 11:52 PM
Points: 62,
Visits: 118
|
|
can do by for xml element . and then eliminate traling ',' with stuff. no need of a variavle to store.direcectly can return that.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Today @ 6:39 AM
Points: 15,
Visits: 190
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Saturday, October 06, 2012 9:30 AM
Points: 9,
Visits: 33
|
|
Definitely use XML over the variable-assignment syntax, as SQL Server supports XML. The variable-assignment syntax is not documented. It works, but is not supported.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 11:42 PM
Points: 424,
Visits: 55
|
|
|
|
|