Paging statements in SQL Server databases

Source: Internet
Author: User

Two paging SQL statements

The first one is the. Not in

 

Select Top 2 * From xy_role where id not in (select Top 4 ID from xy_role)

 

'2' indicates the number of information entries displayed on each page. '4': for example, if the current page is 3 pages, it is (3-1) * (each number)

Remember to sort and filter the data in the subquery if you want to sort the data.

 

Type 2: Use the row_number () function in sqlserver2005 or later versions ()

Select Top 2 * from (select *, row_number () over (order by id desc) as Ro from xy_role) as awhere A. Ro> 4 order by ID DESC

Row_number () is used to arrange data in a sequential number, such as 1, 2, 3, 4, 5... in this case, Table A has a column named Ro,

Use Where a. Ro> 4 to get the expected result.

 

The performance should be better.

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.