網上有許多帶排序的oracle 分頁儲存過程,但都或多或少有bug,對其改進了許多,可以直
接放在pl/sql developer 中執行,同時感謝網上其他資料.
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;
分頁儲存過程 分頁儲存過程
分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...
分頁儲存過程
create proc p sobigo percentpage tblname varchar 255 t category 表名 strgetfields varchar 1000 需要返回的列 fldname varchar 255 排序的欄位名 pagesize int 10,頁尺寸 pag...
分頁儲存過程
create procedure pro select pageindex int,pagesize int as select student.sno,student.sname,student.s grade.math,grade.physics,grade.huaxue,grade.chine...