常用分页存储过程基本都是SQL SERVER 的,经过试验和总结,将前段时间ORACLE分页控件中的存储过程分享给大家:
--oracle高效分页存储过程(百万数据级)
procedure get_pager
(
parm_pageID in number,--页索引,从1开始
parm_pageSize in number,--每页大小
parm_tblName in varchar2,--表名
parm_strWhere in varchar2,--where条件
parm_orderBy in varchar2, --排序条件
parm_ref_cur1 out ref_cur_type,--总记录数
parm_ref_cur2 out ref_cur_type --结果集
)
IS
v_sql1 VARCHAR2 (2000); --获取数据的sql语句
v_sql2 VARCHAR2 (4000);
v_pagecount NUMBER; --该条件下记录页数
v_row_start NUMBER; --开始记录
v_row_end NUMBER; --结束记录
v_count number;
--分页获取数据
BEGIN
v_sql1 := 'select count(1) from ' || parm_tblName;
IF parm_strWhere is not NULL OR parm_strWhere <> ''
THEN
v_sql1 := v_sql1 || ' where 1=1 ' || parm_strWhere;
END IF;
EXECUTE IMMEDIATE v_sql1 INTO v_count;
OPEN parm_ref_cur1 for v_sql1;
--计算数据记录开始和结束
v_pageCount := v_count / parm_pageSize + 1;
v_row_start := (parm_pageID - 1) * parm_pageSize + 1;
v_row_end := parm_pageID * parm_pageSize;
v_sql2 := 'select rownum ro, t.* from ' || parm_tblName || ' t';
v_sql2 := v_sql2 || ' where rownum<=' || v_row_end;
IF parm_strWhere is not NULL OR parm_strWhere <> ''
THEN
v_sql2 := v_sql2 || parm_strWhere;
END IF;
IF parm_orderBy is not null and parm_orderBy <> ''
THEN
v_sql2 := v_sql2 || ' order by ' || parm_orderBy;
END IF;
v_sql2 := 'select * from (' || v_sql2 || ') where ro >=' || v_row_start;
--DBMS_OUTPUT.put_line (strsql);
OPEN parm_ref_cur2 FOR v_sql2;
END get_pager;
相信学过Oracle的朋友应该不难看懂,呵呵!