MSSQL中怎么实现分页存储过程,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
具体如下:
USE [DB_Common]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[Com_Pagination]@TotalCount INT OUTPUT, --总记录数@TotalPage INT OUTPUT, --总页数@Table NVARCHAR(1000), --查询的表名(可多表,例如:Person p LEFT JOIN TE a ON a.PID=p.Id )@Column NVARCHAR(1000), --查询的字段,可多列或者为*@OrderColumn NVARCHAR(100), --排序字段@GroupColumn NVARCHAR(150), --分组字段@PageSize INT, --每页记录数@CurrentPage INT, --当前页数@Group TINYINT, --是否使用分组,否是@Condition NVARCHAR(4000) --查询条件(注意:若这时候为多表查询,这里也可以跟条件,例如:a.pid=2)ASDECLARE @PageCount INT, --总页数 @strSql NVARCHAR(4000), --主查询语句 @strTemp NVARCHAR(2000), --临时变量 @strCount NVARCHAR(1000), --统计语句 @strOrderType NVARCHAR(1000) --排序语句BEGINSET @PageCount = @PageSize * (@CurrentPage -1)SET @strOrderType = ' ORDER BY ' + @OrderColumn + ' 'IF @Condition != ''BEGIN IF @CurrentPage = 1 BEGIN IF @GROUP = 1 BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' WHERE ' + @Condition + ' GROUP BY ' + @GroupColumn SET @strCount = @strCount + ' SET @TotalCount=@@ROWCOUNT' SET @strSql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @Column + ' FROM ' + @Table + ' WHERE ' + @Condition + ' GROUP BY ' + @GroupColumn + ' ' + @strOrderType END ELSE BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' WHERE ' + @Condition SET @strSql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @Column + ' FROM ' + @Table + ' WHERE ' + @Condition + ' ' + @strOrderType END END ELSE BEGIN IF @GROUP = 1 BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' WHERE ' + @Condition + ' GROUP BY ' + @GroupColumn SET @strCount = @strCount + ' SET @TotalCount=@@ROWCOUNT' SET @strSql = 'SELECT * FROM (SELECT TOP (2000) ' + @Column + ',ROW_NUMBER() OVER(' + @strOrderType + ') AS NUM FROM ' + @Table + ' WHERE ' + @Condition + ' GROUP BY ' + @GroupColumn + ') AS T WHERE NUM BETWEEN ' + STR(@PageCount + 1) + ' AND ' + STR(@PageCount + @PageSize) END ELSE BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' WHERE ' + @Condition SET @strSql = 'SELECT * FROM (SELECT TOP (2000) ' + @Column + ',ROW_NUMBER() OVER(' + @strOrderType + ') AS NUM FROM ' + @Table + ' WHERE ' + @Condition + ') AS T WHERE NUM BETWEEN ' + STR(@PageCount + 1) + ' AND ' + STR(@PageCount + @PageSize) END ENDENDELSE --没有查询条件BEGIN IF @CurrentPage = 1 BEGIN IF @GROUP = 1 BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' GROUP BY ' + @GroupColumn SET @strCount = @strCount + 'SET @TotalCount=@@ROWCOUNT' SET @strSql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @Column + ' FROM ' + @Table + ' GROUP BY ' + @GroupColumn + ' ' + @strOrderType END ELSE BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table SET @strSql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @Column + ' FROM ' + @Table + ' ' + @strOrderType END END ELSE BEGIN IF @GROUP = 1 BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' GROUP BY ' + @GroupColumn SET @strCount = @strCount + 'SET @TotalCount=@@ROWCOUNT' SET @strSql = 'SELECT * FROM (SELECT TOP (2000) ' + @Column + ',ROW_NUMBER() OVER(' + @strOrderType + ') AS NUM FROM ' + @Table + ' GROUP BY ' + @GroupColumn + ') AS T WHERE NUM BETWEEN ' + STR(@PageCount + 1) + ' AND ' + STR(@PageCount + @PageSize) END ELSE BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table SET @strSql = 'SELECT * FROM (SELECT TOP (2000) ' + @Column + ',ROW_NUMBER() OVER(' + @strOrderType + ') AS NUM FROM ' + @Table + ') AS T WHERE NUM BETWEEN ' + STR(@PageCount + 1) + ' AND ' + STR(@PageCount + @PageSize) END ENDENDEXEC sp_executesql @strCount, N'@TotalCount INT OUTPUT', @TotalCount OUTPUTIF @TotalCount > 2000BEGIN SET @TotalCount = 2000ENDIF @TotalCount%@PageSize = 0BEGIN SET @TotalPage = @TotalCount / @PageSizeENDELSEBEGIN SET @TotalPage = @TotalCount / @PageSize + 1ENDSET NOCOUNT ONEXEC (@strSql)ENDSET NOCOUNT OFF
看完上述内容,你们掌握MSSQL中怎么实现分页存储过程的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!