C # implement pagination of query results using SQL statements without using gridview

Source: Internet
Author: User

If I have 100 records, how can I use SQL Server's segmented query to display 10 records per page and 10 pages?

I:

Select top 10 * from table where primary key not in (select top 10 * (page-1) primary key from table)

In this case, the table is the name of your table. The primary key is the primary key field of the table. The number of pages should be dynamically set. You can determine the number of pages based on the pages and change them accordingly.

II:

Assume that the primary key column is ID

Select top 10 * from Table order by ID (first page, first 10)

Each time the next page is followed, an incremental number is required to determine the page number ~ Assume it is A, the second page is 1, and the third page is 2

Select top 10 * from table whereIDNot in (select top 10 *
IDFrom table order by ID)

The two red values must correspond to each other. Otherwise, the error"Only one expression can be specified in the select list when the subquery is not introduced
With exists."

Original article address

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.