Even then, there is no guarantee that the returned rows will be in the order of the clustered index. The fact that you observe that the rows in a particular resultset are ordered by the clustered index in a simple table and simple query absent any ORDER BY clause does not mean it always happens that way.
The problem with the (idiomatic) statement:
SELECT @stringvar = @stringvar + ', ' + somecolumn
FROM sometable
is there is no way to
guarantee that the various rows will be returned in any particular order, clustered index notwithstanding. The (internal) execution of this statement happens way before the resultset is constructed for return back to the client, so it is highly dependant on the execution plan.
Granted, in this very simple example, it almost certainly will be in order by the clustered index. But add a JOIN or two, a subquery or view, maybe an index or two on some other columns, or on the column in question, choose an unusual distribution of data values, have the data in the table be very active (or pinned) so it is cached, etc. and the optimizer may very well decide on a different execution plan and return the rows in some other order. By definition it is free to do that, and there is not a thing you can do about it.
Look. I admit to being pedantic here. You could probably run such a query a gazillion times under all sorts of circumstances and every time you observe it, you see the results in a predictable order.
But, upgrade to the next version, change from SQL Server to some other implementation, and you could be surprised. If the risk of a sudden change in behavior is acceptable, then by all means do what works for you. Just don't complain when you use undocumented behavior and it someday no longer works the way you'd like...
On another pedantic note, it is all I can do to restrain myself from asking why the OP would want to return a result that violated first normal form (atomic data)? It sounds like he is trying to resolve a presentation issue in the database, precisely where it doesn't belong....
Jeff Mason
Custom Apps, Inc.
[email protected]