create or replace procedure PageOracle
(
p_pageSize in number, --输入参数:每页记录数
p_currentPage in number, --输入参数:当前页
p_sql in varchar2 , --查询语句
v_maxPage out number, --总页数--
v_totalRows out number,--总记录数--
result_cursor out SYS_REFCURSOR --结果集
) is
v_sql_totalRows varchar2(3000);
v_rowStart number; --开始行--
v_rowEnd number; ---结束行--
v_mod number;
v_query_sql varchar2(3000);
begin
----查询出符合条件的总记录数--
v_sql_totalRows := 'select count(*) from ( ' || p_sql || ')';
execute immediate v_sql_totalRows into v_totalRows;
select mod(v_totalRows,p_pageSize) into v_mod from dual;
if v_mod = 0 then
v_maxPage := v_totalRows/p_pageSize;
else
v_maxPage := ((v_totalRows-v_mod)/p_pageSize+1);
end if;
v_rowStart :=(p_currentPage-1)*p_pageSize+1;
v_rowEnd :=p_currentPage*p_pageSize;
v_query_sql :=' select * from (
select A.*,rownum rn
from (select * from '||p_sql || ') A
where rownum < = '||v_rowEnd||
') where rn > = ' ||v_rowStart;
open result_cursor for v_query_sql;
end PageOracle;