Tips on using top + temporary tables for paging

Source: Internet
Author: User
Today, we found that there is a paging problem on the list page in the system. Some records cannot be displayed. The list page uses top + temporary table SQL. The error is:

When you click to sort fields of the int type, some records cannot be found on all pages,

Run the SQL statement explain and find that the order of records obtained by the order by statement for the top + quantity is different.

Rows with different sequences have the same values, for example:

Select top 10 * From Table1 order [count] ASC

The result is as follows:

ID Count
13 1
1 2
2 2
56 4
6 5
8 6
9 8
7 7
45 7

Select top 11 * From Table1 order [count] ASC

ID Count
13 1
1 2
2 2
56 4
6 5
8 6
9 6
45 7
7 7
456 7

We found that the location of the two red records has changed. It is used in paging technology. If pagesize is 9, one row cannot be displayed.

Later, we set the Data Type of the count column to Char and OK. The same order is obtained each time. No matter how many top ....

After thinking for half a day, the only reason may be that SQL Server has different policies when the execution plan has different top numbers ..

Solution:

Add a sorting field to order by, as long as it is not an int.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.