Oracle分頁的儲存過程 返回頁總數

2021-05-24 00:39:19 字數 3572 閱讀 2134

create or replace package autopagenation is

-- author  : mark

-- created : 2010-12-30 11:23:18

-- purpose :

-- public type declarations

-- 返回結果集的游標

type typecousor is ref cursor;

-- 分頁的儲存過程

procedure pagenation(

tblname in varchar2, --查詢的表名

strfields in varchar2,--查詢的字段

strsortedfield in varchar2,--排序的字段

sorttype in number,--排序的型別,1為desc,預設為asc

pagesize in number,--分頁的容量,預設為20條/頁

pageindex in number,--要取的頁碼,預設為第1頁

strwhere in varchar2,--查詢條件,不要寫where

recordcount out varchar2,--輸出值,記錄總數

pagecount out number,--輸出值,頁總數

selrecords out typecousor

);end autopagenation;

create or replace package body autopagenation is

procedure pagenation(

tblname in varchar2, --查詢的表名

strfields in varchar2,--查詢的字段

strsortedfield in varchar2,--排序的字段

sorttype in number,--排序的型別,1為desc,預設為asc

pagesize in number,--分頁的容量,預設為20條/頁

pageindex in number,--要取的頁碼,預設為第1頁

strwhere in varchar2,--查詢條件,不要寫where

recordcount out varchar2,--輸出值,記錄總數

pagecount out number,--輸出值,頁總數

selrecords out typecousor --輸出的結果集

) is

tmpsorttype varchar2(10);

tmppagesize number;

tmppageindex number;

tmpstrsqlcount varchar2(500);

tmpstrsql varchar2(500);

tmpstrrecodsql varchar2(500);

startpagedataindex number;

endpagedataindex number;

tmppagecount number;

begin

--排序的型別

if sorttype = 1 then

tmpsorttype := ' desc';

else

tmpsorttype := ' asc';

end if;

--分頁容量

if pagesize is null or pagesize = 0 then

tmppagesize := 20;

else

tmppagesize := pagesize;

end if;

--要取的頁碼

if pageindex is null or pageindex = 0 then

tmppageindex := 1;

else

tmppageindex := pageindex;

end if;

--查詢記錄

if strwhere is null or strwhere = ' ' then

begin

tmpstrsqlcount := 'select count(*) from '||tblname;

if strfields = '*' then

tmpstrsql := 'select t.* from '||tblname||' t order by '||strsortedfield||tmpsorttype;

else

tmpstrsql := 'select '||strfields||' from '||tblname||' order by '||strsortedfield||tmpsorttype;

end if;

end;

else

begin

tmpstrsqlcount := 'select count(*) from '||tblname||' where '||strwhere;

if strfields = '*' then

tmpstrsql := 'select t.* from '||tblname||' t where '||strwhere||' order by '||strsortedfield||tmpsorttype;

else

tmpstrsql := 'select '||strfields||' from '||tblname||' where '||strwhere||' order by '||strsortedfield||tmpsorttype;

end if;

end;

end if;

--execute immediate tmpstrsqlcount into recordcount;

--- 定義始終頁碼的資料位置

startpagedataindex := tmppagesize* ( tmppageindex -1)+1;

endpagedataindex := tmppageindex * tmppagesize;

---求頁總數

tmppagecount := mod(recordcount,tmppagesize);--求餘運算

if tmppagecount >0 then

tmppagecount := 1;

else

tmppagecount := 0;

end if;

tmppagecount := trunc(recordcount/tmppagesize) +tmppagecount;

pagecount := tmppagecount;

--取出頁碼定義的結果集

tmpstrrecodsql := 'select * from  (select rownum rowno,tmp.* from ('||tmpstrsql||') tmp  where rownum

<='||endpagedataindex|| ')  where rowno >= '||startpagedataindex;

open selrecords for tmpstrrecodsql;

end pagenation;

end autopagenation;

oracle分頁儲存過程

page slide procedure author robert.c time 2006.11.17 create or replace procedure tablepage select v page size int,the size of a page of list v current...

Oracle分頁儲存過程

第一步要先建立包 create or replace package pkg query is type cur query is ref cursor procedure met down query m tablename in varchar2,表名 m strwhere in varchar...

Oracle分頁儲存過程

第一步要先建立包 create or replace package pkg query is type cur query is ref cursor procedure met down query m tablename in varchar2,表名 m strwhere in varchar...