Using Row_number in Oracle to realize paging

Source: Internet
Author: User

1: First of all

Select Row_number () over (order by ID ASC) as ' RowNumber ', * from table1

Generate a collection with an ordinal

2: Re-query the 1th to 5th data of the collection

SELECT * FROM
(select Row_number () over (order by ID ASC) as ' RowNumber ', * from table1) as Temp
where RowNumber between 1 and 5

The full SQL statement

declare @pagesize int; declare @pageindex int; Set @pagesize = 3

Set @pageindex = 1; --First page

SELECT * FROM (select Row_number () over (order by ID ASC) as ' RowNumber ', * from table1) as temp where rowNumber between (( (@pageindex-1) * @pagesize) +1) and (@pageindex * @pagesize)

Set @pageindex = 2; --second page

SELECT * FROM (select Row_number () over (order by ID ASC) as ' RowNumber ', * from table1) as temp where rowNumber between (( (@pageindex-1) * @pagesize) +1) and (@pageindex * @pagesize)

Set @pageindex = 3; --Page Three

SELECT * FROM (select Row_number () over (order by ID ASC) as ' RowNumber ', * from table1) as temp where rowNumber between (( (@pageindex-1) * @pagesize) +1) and (@pageindex * @pagesize)

Set @pageindex = 4;--Fourth page

SELECT * FROM (select Row_number () over (order by ID ASC) as ' RowNumber ', * from table1) as temp where rowNumber between (( (@pageindex-1) * @pagesize) +1) and (@pageindex * @pagesize)

Let's write a stored procedure paging

------------------------------

Alter Procedure Pagepager

@TableName varchar (80),

@File varchar (+),---

@Where varchar,---with and connections

@OrderFile varchar (100),--sort field

@OrderType varchar,--ASC: ORDER, desc: Reverse

@PageSize varchar (10),--

@PageIndex varchar (10)--

As

if (ISNULL (@OrderFile, ') = ')

Begin

Set @OrderFile = ' ID ';

End

if (ISNULL (@OrderType, ') = ')

Begin

Set @OrderType = ' ASC '

End

if (ISNULL (@File, ') = ')

Begin

Set @File = ' * '

End

DECLARE @select varchar (8000)

Set @select = ' SELECT ' + @File + ' from (select *,row_number () over (order by ' + @OrderFile + "+ @Order Type + ') as ' rowNumber ' from ' + @TableName + ' where 1=1 ' + @Where + ') temp where rowNumber between (( ' + @PageIndex + '-1) * ' + @PageSize + ') +1) and (' + @PageIndex + ' * ' + @PageSize + ') '

EXEC (@select)

Using Row_number in Oracle to realize paging

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.