zoukankan      html  css  js  c++  java
  • MSSQL分页:查询前几页速度最快的ROW_NUMBER() OVER分页存贮过程

    CREATE PROCEDURE [dbo].[getPagerROWOVER] 
        @PageIndex int = 1,
        @PageSize int = 10,
        @Tables nvarchar(1000) = NULL,
        @Fields nvarchar(2000) = '*',
        @Where nvarchar(2000) = NULL,
        @GroupBy nvarchar(2000) = NULL,
        @OrderBy nvarchar(1000) = NULL,
        @ReturnCount int = 0 OUTPUT
    AS
    BEGIN
        SET NOCOUNT ON;
        declare @Sql nvarchar(4000),@startRowIndex int, @maximumRows int;
        declare @strWhere nvarchar(2000)
        declare @strGroupBy nvarchar(2000)
    
        set @startRowIndex = (@PageIndex - 1) * @PageSize;
        set @maximumRows = @PageSize;
    
        IF @Where IS NOT NULL AND @Where != ''
        BEGIN
            SET @strWhere = ' WHERE ' + @Where
        END
        ELSE
        BEGIN
            SET @strWhere = ''
        END
        IF @GroupBy IS NOT NULL AND @GroupBy != ''
        BEGIN
            SET @strGroupBy = ' GROUP BY ' + @GroupBy
        END
        ELSE
        BEGIN
            SET @strGroupBy = ''
        END
            
        set @Sql = 'select ' + @Fields + ' from (select ' + @Fields + ', ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') as rownum from ' + @Tables + @strWhere + @strGroupBy + ') as tmpTable where rownum > ' + CONVERT(nvarchar(10), @startRowIndex) + ' and rownum <= (' + CONVERT(nvarchar(10), @startRowIndex) + ' + ' + CONVERT(nvarchar(10), @maximumRows) + ')'
        exec(@Sql)
        print @sql
        set @Sql = 'select @ReturnCount=COUNT(*) from ' + @Tables + @strWhere
        exec sp_executesql @Sql,N'@ReturnCount int OUTPUT',@ReturnCount OUTPUT
        print @sql
    END
    
    http://www.cnblogs.com/livexy/archive/2010/07/06/1771936.html
  • 相关阅读:
    CentOS 6.3下Samba服务器的安装与配置(转)
    利用香蕉派自制电视盒子
    利用arduino制作瓦力万年历-1.0
    arduino:int & double 转string 适合12864下使用
    centos 6.X下建立arduino开发环境
    树莓派学习笔记(7):利用bypy实现树莓派NAS同步百度云
    直接插入排序
    直接选择排序
    快速排序算法
    git 分支管理 推送本地分支到远程分支等
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1774499.html
Copyright © 2011-2022 走看看