• 数据库分页存储过程


      整理一下自己在做项目时用到的各数据库分页存储过程
       
       SQL Server2000分页存储过程:
    Create procedure P_Page_Data
    (
    @currentpageindex int=1 ,
    @pagesize int=10,
    @countRecord int output
    )
    As
    declare @currentPageSize int

    DECLARE @sqlStr nvarchar(1000)

    set @currentPageSize=@currentpageindex * @pagesize;
    Begin

    set @sqlStr='select top ' +  Convert(varchar(50),@pagesize) +  ' * from (select top ' + Convert(varchar(50),@currentPageSize) + ' * from computer order by id asc) as tableA order by id desc'

    set @countrecord =(select count(id) from computer)


    EXEC (@sqlStr)
    End 

    DECLARE @countrecord int 
     exec P_Page_Data 1,10,@countrecord OUTPUT

    Sql Server2005分页存储过程:

    Create procedure P_Page_Data
    (
    @currentpageindex int=1 ,
    @pagesize int=10,
    @countRecord int output
    )
    As
    declare @currentPageSize int

    DECLARE @sqlStr nvarchar(1000)

    set @currentPageSize=@currentpageindex * @pagesize;
    Begin

    set @sqlStr='SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY id desc) AS rownum, * FROM computer ) a WHERE rownum >= ' +  Convert(varchar(50),@currentPageSize-@pagesize+1) + 'and rownum <= ' + Convert(varchar(50),@currentPageSize)

    set @countrecord =(select count(id) from computer)
    EXEC (@sqlStr)
    End

    DECLARE @countrecord int
     exec P_Page_Data 2,10,@countrecord OUTPUT

    Oracle10G分页存储过程:
    1.
    create or replace procedure P_PagerSum(p_PageSize number,        --每页记录数
                      p_PageNo number,          --当前页码,从 1 开始
                      p_SqlSelect varchar2,  --查询语句,含排序部分
                      p_OutRecordCount out number,--返回总记录数
                      cur_OUT out GM.Pager.refCursorType)
    as
        v_sql varchar2(3000);
        v_count number;
        v_heiRownum number;
        v_lowRownum number;
    begin
      ----取记录总数
      v_sql := 'select count(*) from (' || p_SqlSelect || ')';
      execute immediate v_sql into v_count;
      p_OutRecordCount := v_count;
      ----执行分页查询
      v_heiRownum := p_PageNo * p_PageSize;
      v_lowRownum := v_heiRownum - p_PageSize + 1;

      v_sql := 'SELECT *
                FROM (
                      SELECT A.*, rownum rn
                      FROM  ('|| p_SqlSelect ||') A
                      WHERE rownum <= '|| to_char(v_heiRownum) || '
                     ) B
                WHERE rn >= ' || to_char(v_lowRownum) ;
                --注意对rownum别名的使用,第一次直接用rownum,第二次一定要用别名rn

      OPEN cur_OUT FOR  v_sql;

    end P_PagerSum;

    2.

    create or replace procedure P_PAGERSUMTEST(p_PageSize number,        --每页记录数
                      p_PageNo number,          --当前页码,从 1 开始
                      p_SqlSelectFirst varchar2, --查询语句,含排序部分
                      p_SqlSelectEnd varchar2,
                      p_OutRecordCount out number,--返回总记录数
                      cur_OUT out GM.Pager.refCursorType)
    as
        v_sql varchar2(4000);
        v_count number;
        v_heiRownum number;
        v_lowRownum number;
    begin
      ----取记录总数
      v_sql := 'select count(*) from (' || p_SqlSelectFirst || p_SqlSelectEnd || ')';
      execute immediate v_sql into v_count;
      p_OutRecordCount := v_count;
      ----执行分页查询
      v_heiRownum := p_PageNo * p_PageSize;
      v_lowRownum := v_heiRownum - p_PageSize + 1;

      v_sql := 'SELECT *
                FROM (
                      SELECT A.*, rownum rn
                      FROM  ('|| p_SqlSelectFirst || p_SqlSelectEnd || ') A
                      WHERE rownum <= '|| to_char(v_heiRownum) || '
                     ) B
                WHERE rn >= ' || to_char(v_lowRownum) ;
                --注意对rownum别名的使用,第一次直接用rownum,第二次一定要用别名rn

      OPEN cur_OUT FOR  v_sql;

    end P_PAGERSUMTEST;

    Oracle10G的分页存储过程有两个之间的区别只是将请求的SQL语句分半,防止传递超长SQL语句(4000个字符以上时,测试时超过三千个字符都报错误)时ORACLE报错误。这个问题在项目中可苦恼了很长时间,都无法解决,最后才想到可以将SQL语句分段处理。




  • 相关阅读:
    【bzoj2733】永无乡(无旋treap启发式合并 + 并查集)
    【bzoj2002】弹飞绵羊(分块)
    【bzoj2724】蒲公英(分块)
    【最大M子段和】dp + 滚动数组
    【最大连续子段和】单调队列 + 前缀和优化
    【广告印刷】单调队列
    【烽火传递】dp + 单调队列优化
    【志愿者选拔】单调队列、输入优化
    【Sliding Window】单调队列
    【序列操作V】平衡树(无旋treap)
  • 原文地址:https://www.cnblogs.com/witer666/p/938367.html
Copyright © 2020-2023  润新知