http://zzx0421.javaeye.com/blog/281015
Oracle 分頁:
create or replace procedure P_QuerySplit(
sqlscript varchar2, 表名/SQL語句
pageSize integer, 每頁記錄數
pageIndex integer, 當前頁
totalCount out number, 總記錄數
totalPage out number, 總頁數
v_cur out sys_refcursor 返回游標
- ) is
- /**
- * by chenjianxin 2008-5-3
- *
- */
- v_PageSize number;
- v_PageIndex number;
- v_SQL_Count varchar2(4000);
- v_SQL varchar2(4000);
- v_StartIndex number;
- v_EndIndex number;
- begin
- v_PageSize:=pageSize;
- if v_PageSize=0 then
- v_PageSize:=1;
- end if;
ibatis調用Oracle分頁存儲過程中需要統計記錄數量
- v_SQL_Count := 'select count(*) from (' ? ? sqlscript ? ?') a ';
- execute immediate v_SQL_Count into totalCount;
計算總頁數
- totalPage:=CEIL(totalCount/v_PageSize);
驗證頁號 如果頁號大余了最大頁數,返回最后一頁
- v_PageIndex:=pageIndex;
- if v_PageIndex>totalPage then
- v_PageIndex:=totalPage;
- end if;
計算開始的Index和結束的Index
- v_StartIndex:=(v_PageIndex-1)*v_PageSize 1;
- v_EndIndex:=v_PageIndex*v_PageSize;
- v_SQL:='SELECT /* FIRST_ROWS */* FROM (';
- v_SQLv_SQL:=v_SQL ? ?' SELECT A.*, ROWNUM RN ';
- v_SQLv_SQL:=v_SQL ? ?' FROM (' ? ?sqlscript ? ?') A ';
- v_SQLv_SQL:=v_SQL ? ?' WHERE ROWNUM <= ' ? ?v_EndIndex;
- v_SQLv_SQL:=v_SQL ? ?')WHERE RN >= ' ? ?v_StartIndex;
- open v_cur for v_SQL;
- end P_QuerySplit;
create or replace procedure PageInation(
p_CURSOR out TESTPACKAGE.Test_CURSOR,
tableName in varchar2,
tableResult in varchar2,
lowerNum in numeric,
higherNum in numeric
) is
sqls varchar2(2000);
begin
sqls :='select * from ( select rownum rownum_,'||tableResult||' from ('||tableName||') row_ where rownum <=' ||higherNum||') where rownum_ >'||lowerNum;
OPEN p_CURSOR FOR sqls;
end PageInation;