• Christian Buettner-167247 (12/14/2009)


    Same code as before, but positioned order by (1 instead of i)

    select @txt = @txt + '-' + a from #xxx order by 1

    Not the same code. Your code is equal to select @txt = @txt + '-' + a from #xxx order by @txt + '-' + a.

    Looks like SQL Server doesn't use recursive assignment in this case. SQL Server cannot change the value of a variable and, at the same time, sort data using this volatile variable.

    When "order by i" is used, SQL Server can assign values to the variable recursively.

    This is just my guess 🙂