您好,欢迎访问一九零五行业门户网

Oracle存储过程分页查询

以下是oracle存储过程分页查询返回数据集的方法: create or replace procedure proc_get_data_paging(p_tablename in varcha
以下是oracle存储过程分页查询返回数据集的方法:
create or replace procedure proc_get_data_paging(p_tablename    in varchar2, --表(视图)名
                                                p_strwhere    in varchar2, --查询条件
                                                p_ordercolumn  in varchar2, --排序的列
                                                p_curpage      in out number, --当前页
                                                p_pagesize    in out number, --每页显示记录条数
                                                p_totalrecords out number, --总记录数
                                                p_totalpages  out number, --总页数
                                                v_cur          out types.cursortype) --返回的结果集
 is
  v_sql        varchar2(4000) := ''; --sql语句
  v_startrecord number(10); --开始显示的记录条数
  v_endrecord  number(10); --结束显示的记录条数
  v_showall    integer; --是否显示全部记录
begin
  --记录中总记录条数
  v_sql := 'select to_number(count(*)) from ' || p_tablename ||
          ' where 1=1 ';
  if p_strwhere is not null or p_strwhere '' then
    v_sql := v_sql || p_strwhere;
  end if;
  execute immediate v_sql
    into p_totalrecords;
  --验证页面记录大小
  if p_pagesize     v_showall  := 1;
    p_pagesize := 0;
  end if;
  if v_showall is null then
    --根据页大小计算总页数
    if mod(p_totalrecords, p_pagesize) = 0 then
      p_totalpages := trunc(p_totalrecords / p_pagesize, 0);
    else
      p_totalpages := trunc(p_totalrecords / p_pagesize, 0) + 1;
    end if;
else
    p_totalpages := 1;
end if;
  --验证页号
  if p_curpage     p_curpage := 1;
  end if;
  if p_curpage > p_totalpages then
    p_curpage := p_totalpages;
  end if;
  --实现分页查询
  v_startrecord := (p_curpage - 1) * p_pagesize + 1;
  v_endrecord  := p_curpage * p_pagesize;
  v_sql        := 'select * from (select a.*, rownum r from ' ||
                  '(select * from ' || p_tablename;
  if p_strwhere is not null or p_strwhere '' then
    v_sql := v_sql || ' where 1=1 ' || p_strwhere;
  end if;
  if p_ordercolumn is not null or p_ordercolumn '' then
    v_sql := v_sql || ' order by ' || p_ordercolumn;
  end if;
  if v_showall is null then
    v_sql := v_sql || ') a where rownum             ') b where r >= ' || v_startrecord;
  else
    v_sql := v_sql || ') a ) b ';
  end if;
  dbms_output.put_line(v_sql);
  open v_cur for v_sql;
end proc_get_data_paging;

其它类似信息

推荐信息