Four ways to paging SQL Server

Source: Internet
Author: User

The first type: Row_number () over () mode

SELECT * FROM (
SELECT *, Row_number () over (Order by ArtistID) as RowId from Artistmodels
) as B

where RowId between and 20
  ---where RowId between the current number of pages -1* and pages * Number of bars---

The execution results are:

The second way: offset fetch next mode (SQL2012 more than the version is supported: recommended)

SELECT * from Artistmodels ORDER by ArtistID offset 4 rows fetch next 5 rows only
--order by ArtistID offset page rows fetch Next number rows only----

The execution results are:

Third Way:--top not in mode (adapts to versions below database 2012)

Select Top 3 * from Artistmodels
where ArtistID not in (select Top ArtistID from Artistmodels)

------where Id not in (select Top Bar * Number of pages ArtistID from artistmodels)

Execution Result:

Fourth Way: Paging with stored procedures

CREATE procedure Page_demo
@tablename varchar (20),
@pageSize int,
@page int
As
DECLARE @newspage int,
@res varchar (100)
Begin
Set @[email protected]* (@page-1)
Set @res = ' select * from ' [e-mail protected]+ ' ORDER by ArtistID offset ' +cast (@newspage as varchar) + ' rows fetch NEX T ' + CAST (@pageSize as varchar) + ' rows only '
EXEC (@res)
End
EXEC Page_demo @tablename = ' artistmodels ', @pageSize =3, @page =5

Execution Result:

PS: Today engaged in the afternoon of the page, through the Internet to check the data and their own experiments, summed up four ways for everyone to refer to the page, there are problems for everyone to exchange learning

Four ways to paging SQL Server

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.