分頁儲存過程

紅葉DBA發表於2011-04-12

-- 分頁儲存過程:

-- 編寫一個分頁儲存過程,要求:
--輸入:表名,每頁顯示記錄數,當前頁;
--輸出:總記錄數,總頁數,結果集;

create or replace package mypackage as
type rescursor is ref cursor;
end mypackage;

create or replace procedure fenye
(ptable in varchar2,
pnum in number,
ppage in number,
anum out number,
apage out number,
aresult out mypackage.rescursor)
is
v_sql varchar2(111);
v_pagebegin number:=pnum*(ppage-1)+1;
v_pageend number:=pnum*ppage;
begin
v_sql:='select * from (select a1.*,rownum rn from (select * from' || ptable ||
') a1 where rownum<='|| v_pageend ||') where rn>'|| v_pagebegin;
open aresult for v_sql;
v_sql:='select count(*) from'|| ptable;
execute immediate v_sql into anum;
apage:=(anum+pnum-1)/pnum;
close aresult;
end;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24465008/viewspace-692335/,如需轉載,請註明出處,否則將追究法律責任。

相關文章