We appear to have just experienced the same problem as Ian describes at the beginning of this thread.
We are in the final stages of testing the next release of our application. One of our test scenarios is to validate the sort order of our paged grids (yeah, someone has to do it manually at the moment
poor sucker). We have not had a problem previously but in this round of testing the sort order failed in one particular case.
We're using SQLServer 2000 and llblgen 1.2004.2 about box: July 13th 2005.
This particular query has a large set of result fields and > 150 result rows.
We captured the llblgen generated query in query analyser and did some disecting and analysis on that query.
The paging query does this:
- it creates a temptable
- it inserts the resultset of the query executed (up till the page requested) into this temptable, with a new PK, which is an identity
- it then selects from the temptable the actual page to return, using the identity PK field.
We independently ran the query that produces the resultset that is inserted into the temp table. That sorted correctly.
We then selected the values manually out of the temp table. The order was wrong.
We also selected out the __rowcnt PK field from the temp table along with our results and got the following unexpected and interesting results (this is a partial extract - original query had order by STREETNAME):
__rowcnt,STREETNAME
146,Auckland St
147,Auckland St
148,Butler Dr
149,Butler Dr
150,Butler Dr
141,Adelaide St
142,Adelaide St
143,Adelaide St
144,Adelaide St
145,Auckland St
So the values coming out of the temp table are NOT in the same order as they were inserted
!!!
So we did this to the paging query:
FROM #TempTable
WHERE [__rowcnt] > @__rownoStart AND [__rowcnt] <= @__rownoEnd
ORDER BY [__rowcnt]
And of course this time got the right results.
But then as you say in this thread:
Ok here's the thing.
By default, the rows in a select are unorded, and can be returned in any order by the database system.
Unfortunately it looks like that goes for temp tables as well which has a remote chance of screwing up sort order in paging.
I'll email you our query and results so you can take a look if you'd like.