Best practice for pagination in Oracle?

前端 未结 7 1384
别那么骄傲
别那么骄傲 2020-11-28 07:22

Problem: I need write stored procedure(s) that will return result set of a single page of rows and the number of total rows.

Solution A: I create tw

7条回答
  •  無奈伤痛
    2020-11-28 08:20

    In Oracle 12C you can use limit LIMIT and OFFSET for the pagination.

    Example - Suppose you have Table tab from which data needs to be fetched on the basis of DATE datatype column dt in descending order using pagination.

    page_size:=5
    
    select * from tab
    order by dt desc
    OFFSET nvl(page_no-1,1)*page_size ROWS FETCH NEXT page_size ROWS ONLY;
    

    Explanation:

    page_no=1 page_size=5

    OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY - Fetch 1st 5 rows only

    page_no=2 page_size=5

    OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY - Fetch next 5 rows

    and so on.

    Refrence Pages -

    https://dba-presents.com/index.php/databases/oracle/31-new-pagination-method-in-oracle-12c-offset-fetch

    https://oracle-base.com/articles/12c/row-limiting-clause-for-top-n-queries-12cr1#paging

提交回复
热议问题