oracle 存储过程分页模板

create or replace package pck_users as  
type user_cursor is ref cursor;  
end   pck_users

create or replace  procedure fenye (
		tableName in varchar2, --表名
		pageIndex in number,  --显示的页数
		pagetotal in number,  --每页显示的条数
		sortName in varchar2,--排序的字段
		sortDesc in varchar2,--升序还是降序
		pageCount out number,--总的页数
		totalCount out number, --总的数量,
		p_cursor out pck_users.user_cursor, --返回游标
		resut_code out number  --状态码
		)
is
--定义部分
v_begin number:=((pageIndex*pagetotal)-pagetotal)+1;--从那个位置开始查询
v_end number:=pageIndex*pagetotal;
v_sql varchar(2000); --执行的sql语句
--执行部分
begin
	v_sql:='select * from  (select t.*,rownum rn from 
	(select * from '|| tableName ||' order by '|| sortName||' '||sortDesc ||') t1 where rownum<='|| v_end ||') 
	where rn>='||v_begin ;
	open p_cursor for v_sql;--打开游标
       --查询总条数
       select count(*) into totalCount from tableName;
       --这样也行
       /*
        v_sql:='select count(*) into totalCount from '||tableName;
	execute immediate v_sql into totalCount;
       */
       --计算总的页数 ,用mod函数取余
       if  mod(totalCount,pagetotal)=0 then
	   pageCount:=totalCount/pagetotal;
	else 
	   pageCount:=(totalCount/pagetotal)+1;
	end if;
	close 	p_cursor; --关闭游标
	resut_code:=1; --成功

--异常部分
    exception 
	when other then
	resut_code:=0; --失败
end;
 

猜你喜欢

转载自javaeedevelop.iteye.com/blog/1551293