oracle高效分页存储过程(百万数据级)

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->create or replace procedure Pager(
page in number,--数据页数,从1开始
pageSize in number,--每页大小
tableName nvarchar2,--表名
strWhere nvarchar2,--where条件
Orderby nvarchar2,
numCount out number,--总记录数
v_cur out pkg_query.cur_query) is

strSql varchar2(2000);--获取数据的sql语句
pageCount number;--该条件下记录页数
startIndex number;--开始记录
endIndex number;--结束记录

--作者:王东升
--时间:2010年1月8日14:54:32
--分页获取数据

begin
  strSql:='select count(1) from '||tableName;
  if strWhere is not null or strWhere<>'' then 
     strSql:=strSql||' where '||strWhere;
  end if;  
  EXECUTE IMMEDIATE strSql INTO numCount;
  --计算数据记录开始和结束
  pageCount:=numCount/pageSize+1;
  startIndex:=(page-1)*pageSize+1;
  endIndex:=page*pageSize;

  strSql:='select rownum ro, t.* from '||tableName||' t';  
  strSql:=strSql||' where rownum<='||endIndex;

  if strWhere is not null or strWhere<>'' then 
     strSql:=strSql||' and '||strWhere;
  end if;

  if  Orderby is not null or Orderby<>'' then 
     strSql:=strSql||' order by '||Orderby;
  end if;

  strSql:='select * from ('||strSql||') where ro >='||startIndex;  
  DBMS_OUTPUT.put_line(strSql);

  OPEN v_cur FOR strSql; 
end Pager;

Oracle数据库分页的三种方法

-- 不能对ROWNUM使用>(大于1的数值)、>=(大于或等于1的数值)、=(大于或等于1的数值),否则无结果
-- 所以直接用只能从1开始
-- rownum >10 没有记录,因为第一条不满足去掉的话,第二条的rownum又成了1,所以永远没有满足条件的记录。
select * from student where rownum>=1;

--如果想要用rownum不从1开始,需按下面方法使用
select a1.* from (select student.*,rownum rn from student) a1 where rn >5;


--分页查询一
select * from (select a1.*,rownum rn from (select * from student) a1 where rownum <=5) where rn>=2;

--分页查询二
select a1.* from (select student.*,rownum rn from student where rownum <=5) a1 where rn >=3;

--分页查询三
select a1.* from (select student.*,rownum rn from student) a1 where rn between 3 and 5;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值