ORDER BY suddenly conflicting with VARCHAR concatenation in TSQL

五迷三道 提交于 2019-12-02 03:22:40
Martin Smith

As indicated in the comments you are relying on undocumented behaviour.

Microsoft say "The correct behavior for an aggregate concatenation query is undefined.". If the compute scalar moves to the wrong place in the plan then it just stops working.

The work around you are using of defining the ordering expression in a sub-query and concatenating the results of this query I believe is safe . From Ordering guarantees in SQL Server...

For backwards compatibility reasons, SQL Server provides support for assignments of type SELECT @p = @p + 1 ... ORDER BY at the top-most scope.

That posting was written in 2005 though so I'm not sure if the guarantee still holds (Edit: After reviewing Connect Items on this issue I'm sure that it doesn't)

BTW: You say

I cannot use the XML/STUFF route because some of the data does have < and > signs in it. I'd rather not have to go back to using an old WHILE loop, but I may have to.

This is not the case but it needs a slightly more complicated XML PATH query than you were probably using see the answer here for an example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!