General paging stored procedures for large data volumes in Oracle

Source: Internet
Author: User
Type refcursortype is ref cursor; -- cursor type definition, used to return a dataset

/*************************************** **************************
* Function Description: Common paging stored procedures for large data volumes
* Created by: Xia chuntao xchuntao@163.com QQ: 23106676
* Creation Time:
**************************************** *************************/
Procedure Sp_page (p_pagesize int, -- number of records per page
P_pageno int, -- current page number, starting from 1
P_sqlselect varchar2, -- query statement, including sorting part
P_outrecordcount out int, -- total number of returned records
P_outcursor out refcursortype)
As
V_ SQL varchar2 (3000 );
V_count int;
V_heirownum int;
V_lowrownum int;
Begin
---- Retrieve the total number of records
V_ SQL: = 'select count (*) from ('| p_sqlselect | ')';
Execute immediate v_ SQL into v_count;
P_outrecordcount: = v_count;
---- Query by PAGE
V_heirownum: = p_pageno * p_pagesize;
V_lowrownum: = v_heirownum-p_pagesize + 1;

V_ SQL: = 'select *
From (
Select a. *, rownum Rn
From ('| p_sqlselect |')
Where rownum <= '| to_char (v_heirownum) |'
) B
Where rn> = '| to_char (v_lowrownum );
-- Pay attention to the use of the rownum alias. For the first time, use rownum directly. For the second time, use the alias RN.

Open p_outcursor for v_ SQL;

EndSp_page;

/*************************************** **************************
* Function Description: Common paging stored procedures for large data volumes (heavy load 1, recommended)
* Creator: Xia chuntao
* Creation Time:
**************************************** *************************/
Procedure Sp_page (p_pagesize int, -- number of records per page
P_pageno int, -- current page number, starting from 1
P_sqlselect varchar2, -- query statement, including sorting part
P_sqlcount varchar2, -- query statement for obtaining the total number of records
P_outrecordcount out int, -- total number of returned records
P_outcursor out refcursortype)
As
V_ SQL varchar2 (3000 );
V_count int;
V_heirownum int;
V_lowrownum int;
Begin
---- Retrieve the total number of records
Execute immediate p_sqlcount into v_count;
P_outrecordcount: = v_count;
---- Query by PAGE
V_heirownum: = p_pageno * p_pagesize;
V_lowrownum: = v_heirownum-p_pagesize + 1;

V_ SQL: = 'select *
From (
Select a. *, rownum Rn
From ('| p_sqlselect |')
Where rownum <= '| to_char (v_heirownum) |'
) B
Where rn> = '| to_char (v_lowrownum );
-- Pay attention to the use of the rownum alias. For the first time, use rownum directly. For the second time, use the alias RN.

Open p_outcursor for v_ SQL;

EndSp_page;

/*************************************** **************************
* Function Description: Paging common stored procedures for large data volumes (heavy load 2)
* Creator: Xia chuntao
* Creation Time:
**************************************** *************************/
Procedure Sp_page (p_pagesize int, -- number of records per page
P_pageno int, -- current page number, starting from 1
P_sqlselect varchar2, -- query statement, including sorting part
P_outcursor out refcursortype)
As
V_ SQL varchar2 (3000 );
-- V_count int;
V_heirownum int;
V_lowrownum int;
Begin
/*
---- Retrieve the total number of records
V_ SQL: = 'select count (*) from ('| p_sqlselect | ')';
Execute immediate v_ SQL into v_count;
P_outrecordcount: = v_count;
*/
---- Query by PAGE
V_heirownum: = p_pageno * p_pagesize;
V_lowrownum: = v_heirownum-p_pagesize + 1;

V_ SQL: = 'select *
From (
Select a. *, rownum Rn
From ('| p_sqlselect |')
Where rownum <= '| to_char (v_heirownum) |'
) B
Where rn> = '| to_char (v_lowrownum );
-- Pay attention to the use of the rownum alias. For the first time, use rownum directly. For the second time, use the alias RN.

Open p_outcursor for v_ SQL;

EndSp_page;

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.