oracle 存储过程分页模板

本文详细介绍了SQL分页查询的原理与实现方法,包括如何通过调整参数优化查询效率,以及具体步骤来实现分页功能。通过实例展示,帮助开发者理解和掌握分页查询的最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值