Oracle分页

create or replace package datapagination is

  -- Author  : 张林春
  -- Created : 2008-12-30 14:53:24
  -- Purpose : 大数据量分页
 
  -- Public type declarations
  type M_Cursor is ref cursor;
 
  -- Public function and procedure declarations
  procedure pagination(
       myCursor out M_Cursor,
       tblName in varchar, -- 表名
       strGetFields in varchar := '*', -- 需要返回的列
       fldName in varchar:='', -- 排序的字段名
       PageSize in int , -- 页尺寸
       PageIndex in int, -- 页码
       doCount in int , -- 返回记录总数, 非 0 值则返回
       OrderType in int , -- 设置排序类型, 非 0 值则降序
       strWhere in varchar:=null -- 查询条件 (注意: 不要加 where)
       --mresult out varchar
  );
end datapagination;

-------------------------------------------------------

create or replace package body datapagination
is
 procedure pagination
 (
       myCursor out M_Cursor,
       tblName in varchar, -- 表名
       strGetFields in varchar := '*', -- 需要返回的列
       fldName in varchar:='', -- 排序的字段名
       PageSize in int , -- 页尺寸
       PageIndex in int, -- 页码
       doCount in int , -- 返回记录总数, 非 0 值则返回
       OrderType in int , -- 设置排序类型, 非 0 值则降序
       strWhere in varchar:=null -- 查询条件 (注意: 不要加 where)
     --  mresult out varchar
 )
 is
   v_sql varchar2(1000);--主语句
   v_strTmp varchar2(110) ;-- 临时变量
   v_strOrder varchar2(400);-- 排序类型
 begin
   if doCount!=0 then
      if strWhere is null then
         v_sql:='select count(*) as Total from '||tblName ;
      else
         v_sql:='select count(*) as Total from '||tblName||' where '+strWhere ;
      end if;
   else
      --如果OrderType不是0,就执行降序,这句很重要!
      if OrderType!=0 then
          v_strTmp:='<(select min';
          v_strOrder:=' order by '||fldName||' desc';
      else
          v_strTmp:='<(select max';
          v_strOrder:=' order by '||fldName||' asc';
      end if;
      --如果是第一页就执行以下代码,这样会加快执行速度
      if PageIndex=1 then
           if strWhere is null then
              v_sql:='select top '||PageSize||' '||strGetFields||' from '||tblName||' where '||strWhere||' '||v_strOrder;
           else
              v_sql:='select top'||PageSize||' '||strGetFields||' from '||tblName||v_strOrder;
           end if;  
      else
       --以下代码赋予了v_sql以真正执行的SQL代码
       v_sql:='select '||strGetFields||' from '||tblName||' where '||fldName||v_strTmp||'('||fldName||') '||
              'from (
                     select '||fldName||
                     ' from '||tblName||
                     ' where rownum<='||(PageIndex-1)*pagesize||
                     v_strOrder||
                   ' )  tblTmp) and rownum<='||pagesize||v_strOrder;
       if strWhere is not null then
                 v_sql:='select '||strGetFields||' from '||tblName||' where '||fldName||v_strTmp||'('||fldName||') '||
              'from (
                     select '||fldName||
                     ' from '||tblName||
                     ' where '||strWhere||' and rownum<='||(PageIndex-1)*pagesize||
                     v_strOrder||
                   ' )  tblTmp) and '||strWhere||' and rownum<='||pagesize||v_strOrder;
       end if;
         
      end if;
   end if;
/*     v_sql := 'select count(*) from (' || Psqlcount || ')';
      execute immediate v_sql into v_prcount;*/
  open myCursor for v_sql;
  --mresult:=v_sql;
  end pagination;
end datapagination;

--------------------------------------------------------


 

原文地址:https://www.cnblogs.com/yidianfeng/p/1365492.html