一、定義包
create or replace package pg is
-- Author : gjr
-- Created :
-- Purpose :
-- Public type declarations
TYPE T_CURSOR IS REF CURSOR;
--分頁
PROCEDURE GetPageData(
pageIndex integer, --當前頁
pageSize integer, --每頁記錄數
tableName varchar2, --表名
query varchar2, --where語句 和 order by 語句
totalCount out number, --總記錄數
v_cur out T_CURSOR --返回當前頁數據記錄
) ;
end pg;
二、定義包體
create or replace package body pg is
-- Author : gjr
-- Created :
-- Purpose : 分頁
PROCEDURE GetPageData(
pageIndex integer, --當前頁
pageSize integer, --每頁記錄數
tableName varchar2, --表名
query varchar2, --where語句 和 order by 語句
totalCount out number, --總記錄數
v_cur out T_CURSOR --返回當前頁數據記錄
)
AS
v_sql VARCHAR2(5000);
v_Plow number;
v_Phei number;
Psql varchar2(5000);
p_sql varchar2(5000);
v_tmp varchar2(5000);
v_TotalCount varchar2(50); --總記錄數
Begin
--------------------------------顯示任意頁內容
v_Phei := pageIndex* pageSize ;
v_Plow := v_Phei - pageSize + 1;
p_sql:=''select t.* from ''|| tableName ||'' t where 1=1 ''||query||'' '' ; --要求必須包含rownum字段
Psql := ''select rownum rn,a.* from (''|| p_sql ||'') a '';
v_sql := ''select * from ('' || Psql || '') where rn between '' || v_Plow || '' and '' || v_Phei;
v_tmp := ''select count(*) as TotalCount from (''|| p_sql ||'') a '';
execute immediate v_tmp into v_TotalCount;
totalCount := v_TotalCount;
open v_cur for v_sql;
End GetPageData;
end pg;