Oracle分頁儲存過程

2021-08-31 08:00:44 字數 2438 閱讀 1456

第一步要先建立包

create or replace package pkg_query is

type cur_query is ref cursor;

procedure met_down_query(

m_tablename in varchar2,--表名

m_strwhere in varchar2,--查詢條件

m_ordercolumn in varchar2,--排序字段

m_orderstyle in varchar2,--排序方式

m_currpage in out number,--當前頁

m_pagesize in out number,--每頁顯示的記錄數

m_totalrow out number,--總記錄數

m_totalpage out number,--總頁數

v_cur out cur_query --返回結果集

);end pkg_query;

第二步寫儲存過程語句

create or replace procedure met_down_query(

m_tablename in varchar2,--表名

m_strwhere in varchar2,--查詢條件

m_ordercolumn in varchar2,--排序字段

m_orderstyle in varchar2,--排序方式

m_currpage in out number,--當前頁

m_pagesize in out number,--每頁顯示的記錄數

m_totalrow out number,--總記錄數

m_totalpage out number,--總頁數

v_cur out pkg_query.cur_query --返回結果集)is

v_sql varchar2(1000):=''; --sql語句

v_startrecord number(4);--開始記錄數

v_endrecord number(4);--結束記錄數

begin

--總記錄數

v_sql:='select to_number(count(*)) from '||m_tablename||' where 1=1';

if m_strwhere is not null or m_strwhere <>'' then

v_sql:=v_sql || m_strwhere;

end if;

execute immediate v_sql into m_totalrow;

--驗證頁面大小

if m_pagesize<0 then

m_pagesize:=0;

end if;

--根據頁面大小計算總頁數

if mod(m_totalrow,m_pagesize)=0 then

m_totalpage:=m_totalrow/m_pagesize;

else

m_totalpage:=m_totalrow/m_pagesize+1;

end if;

--驗證頁號

if m_currpage<1 then

m_currpage:=1;

end if;

if m_currpage>m_totalpage then

m_currpage:=m_totalpage;

end if;

--實現分頁查詢

v_startrecord:=(m_currpage-1)*m_pagesize+1;

v_endrecord:=m_currpage*m_pagesize;

v_sql:='select * from (select a.*,rownum r from '||

'(select * from '||m_tablename;

if m_strwhere is not null or m_strwhere <> '' then

v_sql:=v_sql || 'where 1=1 '||m_strwhere;

end if;

if m_ordercolumn is not null or m_ordercolumn <> '' then

v_sql:=v_sql || 'order by '||m_ordercolumn||''||m_orderstyle;

end if;

v_sql:=v_sql||')a where rownum<='||v_endrecord||')b where r>='

||v_startrecord;

dbms_output.put_line(v_sql);

open v_cur for v_sql;

end met_down_query;

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 jt p page is type type cur is ref cursor 定義游標變數用於返回記錄集 procedure pagination pindex in number,要顯示的頁數索引,從0開始 psql in varchar2,...