Simple paging script for a single SQL table (continued)

Source: Internet
Author: User

The last time we talked about simple paging, we used not in.

Next we will use Max or min

There are also row_number () system functions above SQL Server 2005

 

Select   *   From Xk_friendlink
-- Retrieve the largest ID
Select   Max (ID) From ( Select   Top   2 ID From Xk_friendlink Order   By ID ASC ) As T
-- Ascending Order
Select   Top   2   *   From Xk_friendlink Where ID > ( Select   Max (ID) From ( Select   Top   2 ID From Xk_friendlink Order   By ID ASC ) As T) Order   By ID ASC

-- Row_number () Auto-increment, non-repetition, no breakpoint in the middle, continuous
Select   * , Row_number () Over ( Order   By ID Desc ) As Rownumber From Xk_friendlink
Select Row_number () Over ( Order   By ID Desc ) As Rownumber From Xk_friendlink

-- Pagesize = 3, pageindex = 2, between pagesize * (PageIndex-1) + 1 and pagesize * pageindex
Declare   @ Pagesize   Int
Set   @ Pagesize = 3
Declare   @ Pageindex   Int
Set   @ Pageindex = 2
Declare   @ Startrow   Int
Set   @ Startrow = @ Pagesize * ( @ Pageindex - 1 ) + 1
Declare   @ Endrow   Int
Set   @ Endrow = @ Pagesize * @ Pageindex
Print   @ Startrow
Print   @ Endrow

Select   *   From ( Select   * , Row_number () Over ( Order   By ID Desc ) As Rownumber From Xk_friendlink)
As TMP Where Rownumber Between   @ Startrow   And   @ Endrow

 

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.