本文实例讲述了MSSQL分页存储过程。分享给大家供大家参考,具体如下:
USE [DB_Common]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE 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)
AS
DECLARE @PageCount INT, --总页数
@strSql NVARCHAr(4000), --主查询语句
@strTemp NVARCHAr(2000), --临时变量
@strCount NVARCHAr(1000), --统计语句
@strOrderType NVARCHAr(1000) --排序语句
BEGIN
SET @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
END
END
ELSE
--没有查询条件
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
END
END
EXEC sp_executesql @strCount,
N'@TotalCount INT OUTPUT',
@TotalCount OUTPUT
IF @TotalCount > 2000
BEGIN
SET @TotalCount = 2000
END
IF @TotalCount%@PageSize = 0
BEGIN
SET @TotalPage = @TotalCount / @PageSize
END
ELSE
BEGIN
SET @TotalPage = @TotalCount / @PageSize + 1
END
SET NOCOUNT ON
EXEC (@strSql)
END
SET NOCOUNT OFF
希望本文所述对大家SQL Server数据库程序设计有所帮助。



