Oracle分頁查詢儲存過程(適用於單錶查詢)

2021-06-22 21:36:09 字數 2013 閱讀 5855

最近在學習儲存過程,網上搜了一些例子,然後自己對照著寫了乙個相對簡單的分頁查詢...

1.建立包

--/create or replace package pkg_pagination as

type pkg_cursor is ref cursor;

end;

/2.呼叫過程

--/create or replace procedure pagination_call(

tabname in varchar2,

pagesize in number,

pagenum in number,

totalrows out number,

totalpage out number,

p_cursor out pkg_pagination.pkg_cursor,

p_where in varchar2,

p_orderby in varchar2,

p_cnsql out varchar2,

p_rssql out varchar2

)asv_sql varchar2(1000);

v_count number;

begin

p_cnsql:='';

p_rssql:='';

v_sql:='from '||tabname||' where 1=1'||p_where;

pagination_count(v_sql,totalrows,p_cnsql);

v_count:=totalrows;

if(v_count>0) then

v_sql:=v_sql||p_orderby;

pagination_result(v_sql,pagenum,pagesize,v_count,totalpage,p_cursor,p_rssql);

else

dbms_output.put_line('no data!');

end if;

end;

/3.查詢返回結果記錄總數過程

--/create or replace procedure pagination_count(

p_sql in varchar2,

totalrows out number,

r_sql out varchar2

)asbegin

r_sql:='select count(*) '||p_sql;

execute immediate r_sql into totalrows;

end;

/4.查詢返回頁結果集過程

--/create or replace procedure pagination_result(

p_sql in varchar2,

pagenum in number,

pagesize in number,

totalrows in number,

totalpage out number,

p_cursor out pkg_pagination.pkg_cursor,

r_sql out varchar2

)asv_begin number:=(pagenum-1)*pagesize+1;

v_end number:=pagenum*pagesize;

begin

r_sql:='select * from (select t1.*, rownum rn from (select * '||p_sql||') t1 where rownum<='||v_end||') where rn>='||v_begin;

open p_cursor for r_sql;

if mod(totalrows,pagesize)=0 then

totalpage:=totalrows/pagesize;

else

totalpage:=totalrows/pagesize+1;

end if;

end;

/(p.s. 初學不久,高手勿噴)

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...