When an ORDER BY clause includes a scalar expression, self-referencing variables in the SELECT clause (e.g. @i = @i + 1) evaluate only once per statement execution rather than once per row. This happens with table variables as well as database tables. My example is simplistic but does a good job of showing the bug. I actually have a production-level need to be building a string that relys on the ORDER BY clause to make the string correct.