一个分页的存储过程

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


-- 获取指定页的数据
ALTER     PROCEDURE pagination
 @tblName NVARCHAR(4000), -- 表名
 @strGetFields NVARCHAR(4000) = '*', -- 需要返回的列
 @fldName NVARCHAR(4000) = '', -- 排序的字段名(可包含如TABLE.FLDNAME形式)
 @PageSize INT = 10, -- 页尺寸
 @PageIndex INT = 1, -- 页码
 @doCount BIT = 0, -- 返回记录总数, 非 0 值则返回
 @OrderType BIT = 0, -- 设置排序类型, 非 0 值则降序
 @strWhere NVARCHAR(1500) = '' -- 查询条件 (注意: 不要加 where)
AS
 DECLARE @strSQL     NVARCHAR(4000) -- 主语句
 DECLARE @strTmp     NVARCHAR(110) -- 临时变量
   DECLARE @strOrder   NVARCHAR(400) -- 排序类型                                  
 DECLARE @strOrder2   NVARCHAR(400) -- 排序类型
 DECLARE @fldName_t  NVARCHAR(255) -- 在分页时用的排序字段名,不包含多表并列时的表名
 SET @fldName_t = RIGHT(@fldName, LEN(@fldName) -CHARINDEX('.', @fldName))
 IF @doCount != 0
 BEGIN
     IF @strWhere != ''
         SET @strSQL = 'select count(*) as Total from ' + @tblName +
             ' where ' + @strWhere
     ELSE
         SET @strSQL = 'select count(*) as Total from ' + @tblName + ''
 END--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
 ELSE
 BEGIN
     IF @OrderType != 0
     BEGIN
        SET @strTmp = '<(select min'
         SET @strOrder = ' order by ' + @fldName + ' desc'
         SET @strOrder2 = ' order by ' + @fldName + ' asc'
             --如果@OrderType不是0,就执行降序,这句很重要!
     END
     ELSE
     BEGIN
         SET @strTmp = '>(select max'
         SET @strOrder = ' order by ' + @fldName + ' asc'
         SET @strOrder2 = ' order by ' + @fldName + ' desc'
     END
     IF @PageIndex = 1
     BEGIN
         IF @strWhere != ''
             SET @strSQL = 'select top ' + STR(@PageSize) + ' ' + @strGetFields
                 + ' from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder
         ELSE
             SET @strSQL = 'select top ' + STR(@PageSize) + ' ' + @strGetFields
                 + ' from ' + @tblName + ' ' + @strOrder
                 --如果是第一页就执行以上代码,这样会加快执行速度
     END
     ELSE
     BEGIN
         --以下代码赋予了@strSQL以真正执行的SQL代码
       
         SET @strSQL = 'select top ' + STR(@PageSize) + ' '+   @strGetFields +
             ' from ' + @tblName + ' where ' + @fldName + ' ' + @strTmp +
             ' (' + @fldName_t + ') from (select top ' + STR((@PageIndex -1) * @PageSize)
             + ' ' + @fldName + ' from ' + @tblName + '' + @strOrder +
             ') as tblTmp)' + @strOrder
        
         IF @strWhere != ''
             SET @strSQL = 'select top ' + STR(@PageSize) + ' ' +  @strGetFields
                 + ' from ' + @tblName + ' where ' + @fldName + ' ' + @strTmp
                 + ' (' + @fldName_t + ') from (select top ' + STR((@PageIndex -1) * @PageSize)
                 + ' ' + @fldName + ' from ' + @tblName + ' where ' + @strWhere
                 + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' +
                 @strOrder
   
    
     /*
     DECLARE @tmpSQL AS NVARCHAR(1000)
     SET @tmpSQL=''
     IF (@strWhere!='')
     BEGIN
      SET @tmpSQL= ' WHERE ' + @strWhere
     END
    
 SET @strSQL = 'SELECT TOP ' + STR(@PageSize) + ' * FROM ( SELECT TOP  ' + STR((@PageIndex -1) * @PageSize) +' '
 + @strGetFields +'  FROM   ' + @tblName + @tmpSQL+ @strOrder+    ') t '+@strOrder2
     */
     END
 END
 PRINT @strSQL
 EXEC (@strSQL)


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

执行例子:exec pagination 'album_view','AlbumID,AlbumName,AlbumNum,AlbumSort,ClassName,ClassID','albumID',10,1,0,1,''

原文地址:https://www.cnblogs.com/Mygirl/p/2206901.html