Oracle分页通用存储过程

create or replace package page is   
type type_cur is ref cursor;   
procedure p_pagintion   
(   
       psql in varchar2,       --条件SQL语句   
       pfrist in number,       --第几页   
       psize in number,        --一页显示多少条   
       pcount out number,      --共几页   
       pnotecount out number,  --总记录   
       pnote out type_cur      --显示页的记录   
);   
END;   
/  
create or replace package body page is   
  
procedure p_pagintion   
(   
       psql in varchar2,       --条件SQL语句   
       pfrist in number,       --第几页   
       psize in number,        --一页显示多少条   
       pcount out number,      --共几页   
       pnotecount out number,  --总记录   
       pnote out type_cur      --显示页的记录   
)   
AS   
v_pfrist number;   
v_sql varchar2(4000);   
v_sql1 varchar2(4000);   
v_sql2 varchar2(4000);   
v_sql3 varchar2(4000);   
v_notecount number;   
v_min number;   
v_max number;   
BEGIN   
            v_sql:='select count(*) from (' || Psql || ')';--查询总记录的SQL   
            execute immediate v_sql into v_notecount;--执行SQL把总记录结果得到   
            pnotecount:=v_notecount;   
            pcount:=ceil(pnotecount/psize);--求出总页数   
            v_pfrist:=pfrist;   
            IF(v_pfrist > pcount)THEN   
            v_pfrist:=pcount; -- 如果传进来的页数大于最后一页,默认返回最后一页数据   
            end IF;   
            v_max:=v_pfrist*psize;--显示页的最大记录排行值   
            v_min:=v_max-psize+1;--显示页的最小记录排行值   
            v_sql1:='select * from (select rownum rn,t.* from ';   
            v_sql2:=' t ) where rn between '||v_min||' and '||v_max;   
            v_sql3:=v_sql1||' ( '||psql||' ) '||v_sql2;--分页SQL   
            open pnote for v_sql3;   
END p_pagintion;   
END page;   
/  
 

猜你喜欢

转载自xiaowu-yang.iteye.com/blog/1628185