SQL Server paging Solution

Source: Internet
Author: User

Paging solution 1: (use not in and select top pages)

Example:

10 records are displayed on each page, and the first page is displayed:

 

Select   Top   10   *   From Production. Product Where
(Productid Not   In ( Select   Top   0 Productid From Production. Product Order   By Productid ))
Order   By Productid

The result is as follows:

 

 

 

10 records are displayed on each page, and the second page is displayed:

Select   Top   10   *   From Production. Product Where  
(Productid Not   In ( Select   Top   10 Productid From Production. Product Order   By Productid ))
Order   By Productid

The result is as follows:

 

It can be summarized:

Select   Top Page size *   From   Table   Where  
(ID Not   In ( Select   Top Page size * (Page number - 1 ) ID From   Table   Order   By ID ))
Order   By ID

 

Paging solution 2: (use the row_number () page added by SQL Server 2005)

Example:

10 records are displayed on each page, and the first page is displayed:

With Products As
( Select  
Row_number () Over ( Order   By Productid) As Rownumber, *
From Production. Product)
Select   *
From Products
Where Rownumber Between   1   And   10 ;

The result is as follows:

 

 

10 records are displayed on each page, and the second page is displayed:

 

With Products As
( Select  
Row_number () Over ( Order   By Productid) As Rownumber, *
From Production. Product)
Select   *
From Products
Where Rownumber Between   11   And   20 ;

The result is as follows:

 

It can be summarized:

 

With Products As
( Select  
Row_number () Over ( Order   By Productid) As Rownumber, *
From Production. Product)
Select   *
From Products
Where Rownumber Between Page size * (Page number - 1 ) + 1   And Page size * Number of pages;

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.