|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article adds further investigation to paging of large resultsets in ASP.NET in: Paging of Large Resultsets in ASP.NET. Have you ever performed paging using a mechanism which is described in Microsoft document? SELECT * FROM PROUCT WHERE ID>@lastProductID
However, there is one prerequisite, the product ID must be an identity column in SQL Server and the statement must not contain Using the codeAssume that we have a product with two columns, one is ID and the other one is price, where ID is a unique identity column, and price is a non-unique and non-nullable column, and we want to sort by price. The SQL is: SELECT * FROM Product ORDER BY Price, ID will give ----------------------------------------
| ID | Price |
----------------------------------------
3 2
4 2
5 2
1 3
2 3
Back to the basics, if we want to display the product without sorting, suppose you have two items in each ASPX page, the SQL of the first page is: SET ROWCOUNT 2 SELECT * FROM PRODUCT WHERER ID>0
And the SQL for the second page (assuming the last product ID in the first page is 2) is: SET ROWCOUNT 2 SELECT * FROM PRODUCT WHERER ID>2
That is easy, but for sorting cause, if we want to sort by price, the equation is: SET ROWCOUNT 2
SELECT * FROM Product WHERE ID>@lastProductID and Price = @lastSortByID
UNION ALL
SELECT * FROM Product WHERE Price > @lastSortByID ORDER BY Price, ID
Or, if you would like, you can use a SET ROWCOUNT 2
SELECT * FROM Product WHERE (ID>@lastProductID and Price = @lastSortByID)
OR (Price > @lastSortByID) ORDER BY Price, ID
For the first page, by applying the equation (assuming -1 is the impossible value for price column): SET ROWCOUNT 2
SELECT * FROM Product WHERE ID>0 and Price = -1
UNION ALL
SELECT * FROM Product WHERE Price > -1 ORDER BY Price, ID
According to the table, there is no records in the first SET ROWCOUNT 2
SELECT * FROM Product WHERE ID>4 and Price = 2
UNION ALL
SELECT * FROM Product WHERE Price > 2 ORDER BY Price, ID
The first That's it. This mechanism works with limitation. The sort by column is not nullable. This is explainable. The null columns cannot be compared using the comparison operator. You cannot write the SQL like " This mechanism is complicated in the client side, as you have to keep track of the view state of the last sort by column value and the last unique column value. I spent quite a lot of time writing the client side code. One special situation that I want to illustrate is when the user is browsing the last item in the last page, and the last page contains only one item. Suddenly, the administrator removes that item, and the user refreshes that page again, then an error is generated. These kind of errors have to be handled as well. Reference:HistoryI am not a very good developer, and if I made something wrong in the article, please correct. Thanks.
|
|||||||||||||||||||||||||||||||||||