[Procedure]Oracle之分页存储过程

来源:互联网 发布:ipad视频软件 编辑:程序博客网 时间:2024/04/30 23:53

关于分页存储过程中使用到的rownum陷阱解析:

http://www.blogjava.net/CONANS/ARTICLES/219693.HTML

create or replace procedure pd_page_query(in_colnames         varchar2,            --列名in_tablename        varchar2,            --表名in_where            varchar2,            --查询条件in_order            varchar2,            --排序条件in_curpage          number,              --当前页in_pagesize         number,              --每页记录数out_totalrecords    out number,          --总记录数out_totalpages      out number,          --总页数out_query_result    out sys_refcursor    --返回的结果集)---------------------------------------------------------------------------------功能描述:通过传入的查询条件返回对应的分页查询结果---时间:    2013-07-10---作者:    sunw---版本:    1.0 beta------------------------------------------------------------------------------isv_start_record int;                --开始显示的记录条数v_end_record   int;                --结束显示的记录条数v_pagesize     int;                --每页记录数v_curpage      int;                --当前页v_colnames     varchar2(2000);     --列名v_where        varchar2(2000);     --查询条件v_order        varchar2(2000);     --排序条件v_count_sql    varchar2(2000);     --记录数语句v_select_sql   varchar2(2000);     --分页语句begin---------------------------------------------------开始判断条件---------------------------------------------------  --如果没有表名,则直接返回异常消息  --如果没有字段,则表示全部字段  if in_colnames is not null then    v_colnames:=in_colnames;  else    v_colnames:='*';  end if;  --如果没有where条件  if in_where is not null  then    v_where:=' WHERE '||in_where;  end if;  --如果没有order by条件  if in_order is null then    v_order:='';  else    v_order:=' ORDER BY '||in_order;  end if;  --如果未指定查询页,则默认为首页  if in_curpage is null or in_curpage<1 then    v_curpage:=1;  else    v_curpage:=in_curpage;  end if;  --如果未指定每页记录数,则默认为10条记录  if in_pagesize is null then    v_pagesize:=10;  else    v_pagesize:=in_pagesize;  end if;---------------------------------------------------开始数据处理----------------------------------------------------  --查询总条数  v_count_sql:='select count(*) from '||in_tablename||v_where;  --执行查询,得到out_totalrecords输出结果  execute immediate v_count_sql into out_totalrecords;  --输出查询语句  dbms_output.put_line('查询总记录数sql=>'||v_count_sql);  dbms_output.put_line('查询总记录数='||out_totalrecords);  --得到总页数,并进行处理  if mod(out_totalrecords,in_pagesize)=0 then    out_totalpages:=out_totalrecords/in_pagesize;  else    out_totalpages:=floor(out_totalrecords/in_pagesize)+1;  end if;  --如果传入的当前页大于最大页  if in_curpage>out_totalpages then    v_curpage:=out_totalpages;  end if;  --设置开始结束的记录数  v_start_record:=(v_curpage-1)*v_pagesize+1;  v_end_record:=v_curpage*v_pagesize;---------------------------------------------------开始构造分页查询语句----------------------------------------------  --构造核心查询语句  v_select_sql:='(select '||v_colnames||' from '||in_tablename||v_where||v_order||') t';  --进行完整的动态sql语句拼写  v_select_sql:='select * from '||                '('||                'select t.*,rownum rn '||                ' from '||                v_select_sql||                ' where rownum<='||v_end_record||                ')'||                ' where rn>='||v_start_record;  --打印完整分页查询语句  dbms_output.put_line('查询sql=>'||v_select_sql);  open out_query_result for v_select_sql;end pd_page_query;


0 0