Summary of four paging methods based on sqlserver and four paging methods of sqlserver

Source: Internet
Author: User

Summary of four paging methods based on sqlserver and four paging methods of sqlserver

First: ROW_NUMBER () OVER () method

Select * from (
Select *, ROW_NUMBER () OVER (Order by ArtistId) AS RowId from ArtistModels
) As B

Where RowId between 10 and 20

--- Where RowId BETWEEN current page number-1 * number of lines and number of pages * number of lines ---

The execution result is:

Method 2: offset fetch next (supported by SQL2012 and later versions: recommended)

Select * from ArtistModels order by ArtistId offset 4 rows fetch next 5 rows only
-- Order by ArtistId offset page number rows fetch next number rows only ----

The execution result is:

Method 3: -- top not in (applicable to versions earlier than database 2012)

Select top 3 * from ArtistModels
Where ArtistId not in (select top 15 ArtistId from ArtistModels)

------ Where Id not in (select top number of pages * ArtistId from ArtistModels)

Execution result:

Method 4: Paging Using Stored Procedures 

CREATE procedure page_Demo
@ Tablename varchar (20 ),
@ PageSize int,
@ Page int
AS
Declare @ newspage int,
@ Res varchar (100)
Begin
Set @ newspage = @ pageSize * (@ page-1)
Set @ res = 'select * from' + @ tablename + 'order by ArtistId offset '+ CAST (@ newspage as varchar (10 )) + 'rows fetch next' + CAST (@ pageSize as varchar (10) + 'rows only'
Exec (@ res)
End
EXEC page_Demo @ tablename = 'artistmodels', @ pageSize = 3, @ page = 5

Execution result:

Ps:This afternoon, I conducted a paging operation. I checked the information online and my experiment. I summarized four paging methods for your reference. If you have any questions, I will share them with you.

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.