ORACLE 分页过程

--创建保存查询结果集的 cursor
create or replace package pkg_query as type cur_query is ref cursor; end pkg_query;
--插件存储过程
create or replace procedure Pagers(
v_cur out pkg_query.cur_query,--查询结果
numCount out number,--总记录数
page in number,--数据页数,从1开始
pageSize in number,--每页大小
tableName varchar2,--表名
strWhere varchar2,--where条件
Orderby varchar2
) is

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

begin

  strSql:='select count(*) 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 Pagers;

posted on 2011-01-11 14:41 himalayas 阅读(195) 评论(0)  编辑  收藏 所属分类: database


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿

随笔分类(15)

随笔档案(16)

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜