编写一个存储过程,输入表名、每页显示的记录数、当前页。返回总记录数,总页数和返回的结果集。
分页sql语句 select * from (select a.*,rownum rn from(select * from emp )a where rownum<=10) where rn>=6;
开始编写分页过程
1.写一个包含游标的包
create or replace package testpackage as
type test_cursor is ref cursor;
end package;
tableName in varchar2, --表名
myPageSize in number, --每页的记录数
pageNow in number, --当前页
myrows out number, --总记录数
myPageCount out number,--总页数
p_cursor out testpackage2.test_cursor) is
--声明开始和结束的记录
v_begin number:=(pageNow-1)*myPageSize+1;
v_end number:=pageNow*myPageSize;
begin
//组织一个sql
open p_cursor for v_sql;
--计算myrows
v_sql:='select count(*) into myrows from '||tableName||'';
execute immediate v_sql into myrows;
--计算myPageCount
if mod(myrows,myPageSize)=0 then
myPageCount:=myrows/myPageSize;
else
myPageCount:=myrows/myPageSize+1;
end if;
//关闭游标
p_cursor;