Oracle笔记:分页查询
查询前三条记录:
select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<=3);
查询第三到六条记录:
select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<=6) where rn>3;
如要指定查询列,排序等,只需修改最里层的子查询.
编写分页存储过程
--编写一个存储过程,要求可以输入表名、每页显示记录数、当前页、
--返回总记录数、总页数和返回的结果集。
create or replace package test_package as
type test_cursor is ref cursor;
end test_package;
create or replace procedure paging
(tableName in varchar2, --表名
recordPerPage in number, --每页显示记录数
currentPage in number, --当前页
totalRecord out number, --总记录数
totalPage out number, --总页数
t_cursor out test_package.test_cursor --返回的结果集
) is
v_sql varchar2(1000);
v_start number:=(currentPage-1)*recordPerPage+1;
v_end number:=currentPage*recordPerPage;
begin
v_sql:='select * from (select a1.*,rownum rn from (select * from '||tableName
||') a1 where rownum<='||v_end||') where rn>='||v_start;
open t_cursor for v_sql;
v_sql:='select count(*) from '||tableName;
--获得总记录数
execute immediate v_sql into totalRecord;
--计算总页数
if mod(totalRecord,recordPerPage)=0 then
totalPage :=totalRecord/recordPerPage;
else
totalPage :=totalRecord/recordPerPage+1;
end if;
close t_cursor;
end;
posted on 2013-03-29 00:00 calliopsis 阅读(149) 评论(0) 编辑 收藏 举报