Oracle 分页存储过程

 1 create or replace package pck_users as
 2 type user_cursor is ref cursor;
 3 end pck_users
 4 
 5 create or replace  procedure fenye (
 6         tableName in varchar2, --表名
 7         pageIndex in number,  --显示的页数
 8         pagetotal in number,  --每页显示的条数
 9         sortName in varchar2,--排序的字段
10         sortDesc in varchar2,--升序还是降序
11         pageCount out number,--总的页数
12         totalCount out number, --总的数量,
13         p_cursor out pck_users.user_cursor, --返回游标
14         resut_code out number  --状态码
15         )
16 is
17 --定义部分
18 v_begin number:=((pageIndex*pagetotal)-pagetotal)+1;--从那个位置开始查询
19 v_end number:=pageIndex*pagetotal;
20 v_sql varchar(2000); --执行的sql语句
21 --执行部分
22 begin
23     v_sql:='select * from  (select t.*,rownum rn from 
24     (select * from '|| tableName ||' order by '|| sortName||' '||sortDesc ||') t1 where rownum<='|| v_end ||') 
25     where rn>='||v_begin ;
26     open p_cursor for v_sql;--打开游标
27        --查询总条数
28        select count(*) into totalCount from tableName;
29        --这样也行
30        /*
31         v_sql:='select count(*) into totalCount from '||tableName;
32     --执行sql语句
33     execute immediate v_sql into totalCount;
34        */
35        --计算总的页数 ,用mod函数取余
36        if  mod(totalCount,pagetotal)=0 then
37        pageCount:=totalCount/pagetotal;
38     else 
39        pageCount:=(totalCount/pagetotal)+1;
40     end if;
41     close     p_cursor; --关闭游标
42     resut_code:=1; --成功
43 
44 --异常部分
45     exception 
46     when other then
47     resut_code:=0; --失败
48 end;
posted @ 2012-08-07 13:06  梦话四叶  阅读(227)  评论(0编辑  收藏  举报