oracle分页存储过程解读。
create or replace package fy_page
is
? ? ?–声明游标
? ?type cursor_page is ref cursor;
end;
–分页存储过程
create or replace procedure
proc_fy
(
????p_tablename varchar2, ? ? ? ? –表名
????p_tablecolumn varchar2, ? ? ? –排序查询列
????p_order varchar2, ? ? ? ? ? ? –排序
????p_pagesize number, ? ? ? ? ? ?–每页大小
????p_curpage number, ? ? ? ? ? ? –当前页
????p_where varchar2, ? ? ? ? ? ? –查询条件
????p_rowcount out number, ? ? ? ?–总条数
????p_pagecount out number, ? ? ? –总页数
????p_cursor out
fy_page.cursor_page
) ? ? –结果集
is
????count_sql varchar2(2000);
????select_sql varchar2(2000);
????start_num number;
????end_num number;
begin
? –查询总条数
? count_sql :=’select count(*) from ‘||p_tablename;
? if p_where is not null then
? ? count_sql := count_sql||’ ‘||p_where;
? end if;
?
? –执行
? execute immediate count_sql into p_rowcount;
? –总页数
? if mod(p_rowcount,p_pagesize)=0 then
? ? p_pagecount := p_rowcount/p_pagesize;
? else
? ? p_pagecount := p_rowcount/p_pagesize+1;
? end if;
? –当前页起始编号
? start_num:=(p_curpage-1)*p_pagesize+1;
? –当前页结束编号
? end_num:=p_curpage*p_pagesize;
? –结果集
? select_sql := ‘select * from (select ‘||p_tablename||’.*,row_number() over(order by ‘||p_tablecolumn||
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?’ ‘||p_order||’) aid from ‘||p_tablename||’ ‘||p_where||’) a where aid between ???????????????????????????’||start_num||’ and ‘||end_num;
? open p_cursor for select_sql;
end proc_fy;