帶排序的oracle分頁儲存過程

2021-05-22 16:05:32 字數 2389 閱讀 4464

create or replace package pager

istype curs is ref cursor;

procedure pagination

(inpagesize in integer, --每頁記錄數

inpageindex in integer, --當前頁數

intablename in varchar2, --表名

inorderfield in varchar2,--排序字段

inisorderby in varchar2,--排序類別,輸入'  desc' 或者' asc'

inwhere in varchar2,--查詢條件

outrecordcount out int, --總記錄數

outpagecount out int,

outcursor out curs --游標變數

);end;

create or replace package body pager

isprocedure pagination

(inpagesize in integer, --每頁記錄數

inpageindex in integer, --當前頁數

intablename in varchar2, --表名

inorderfield in varchar2,--排序字段

inisorderby in varchar2,--排序類別,輸入'  desc' 或者' asc'

inwhere in varchar2,--查詢條件

outrecordcount out int, --總記錄數

outpagecount out int,

outcursor out curs --游標變數)is

v_sql  varchar2(3000); --總的sql語句

v_sql_count varchar2(3000); --總記錄的sql語句

v_sql_order varchar2(2000); --排序的sql語句

v_count int; -- --總記錄數

v_endrownum int; --結束行

v_startrownum int; --開始行

begin

if inorderfield!='no' then

v_sql_order :=' order by '|| inorderfield ||' '||inisorderby;

else

v_sql_order :='';

end if;

if inwhere is not null then

v_sql_count:='select count(rownum) from '||intablename||' where '||inwhere;

else

v_sql_count:='select count(rownum) from '||intablename;

end if;

execute immediate v_sql_count into v_count;

outrecordcount := v_count;

if mod(v_count,inpagesize)=0 then

outpagecount:= v_count/inpagesize;

else

outpagecount:= v_count/inpagesize+1;

end if;

v_startrownum:= 1+(inpageindex-1)*inpagesize;

v_endrownum:= inpageindex*inpagesize;

if inwhere is not null then

v_sql := 'select * from (select '||intablename||'.*, row_number() over ('||v_sql_order||') num  from '||intablename||' where '|| inwhere||'

) where num between '||to_char(v_startrownum)||' and '||to_char(v_endrownum)||'';

else

v_sql := 'select * from (select '||intablename||'.*, row_number() over ('||v_sql_order||') num  from '||intablename||'

) where num between '||to_char(v_startrownum)||' and '||to_char(v_endrownum)||'';

end if;

open outcursor for v_sql;

end;

end;

帶排序的oracle分頁儲存過程

輸入order by 的sqeuence是,應該為 desc 或者 asc 若輸入兩個order by則,v order field a sequence order by b create or replace procedure tablepage select v page size int,...

sql分頁儲存過程,帶求和 排序

create procedure dbo sp tbtest query pagesize int,每頁多少條記錄 pageindex int 1,指定當前為第幾頁 pagecount int output,返回總頁數 recordcount int output,記錄總和 orderfield v...

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