• Erland Sommarskog (7/24/2013)


    Alan.B (7/24/2013)


    Why is the SELECT

    @x=@x+ method not guaranteed to work?

    Why would it?

    See this KB article Pay particular attention to the first sentence under Cause.

    I say it would work based on the example I posted (which works). It produces the exact same plan and answer (except for the leading comma) as what Chris Posted which I believe is guaranteed to work. I need to read the article a little more (as well as this one[/url]) but I think it should work just fine.

    The article you posted seems to imply that there is a problem applying expressions to members of the ORDER BY clause; I am not using an ORDER BY since the requirement does not call for it. From the article:

    Under Symptoms (emph mine):

    You may encounter unexpected results when you apply any operators or expressions to the ORDER BY clause of aggregate concatenation queries.

    Under Cause:

    The correct behavior for an aggregate concatenation query is undefined...

    ... When an expression is applied to a member of an ORDER BY clause, that resulting column is not exposed in the SELECT list, resulting in undefined behavior.

    Under Work Around: (emph mine)

    In order to achieve the expected results from an aggregate concatenation query, apply any Transact-SQL function or expression to the columns in the SELECT list rather than in the ORDER BY clause.

    I could be wrong but I don't believe this article applies.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001