A./*分页存储过程
Descript:分页存储过程
*/
CREATE PROCEDURE database_PageList(
@tblName nvarchar(200), ----要显示的表或多个表的连接
@fldName nvarchar(200) = '*', ----要显示的字段列表
@pageSize int, ----每页显示的记录个数
@page int, ----要显示那一页的记录
@pageCount int = 1 output, ----查询结果分页后的总页数
@Counts int = 1 output, ----查询到的记录数
@fldSort nvarchar(100) = null, ----排序字段列表或条件
@Sort bit, ----排序方法,0为升序,1为降序
@strCondition nvarchar(200), ----查询条件,不需where
@ID nvarchar(50) ----主表的主键
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放动态生成的SQL语句
Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句
Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句
Declare @sqlSort nvarchar(200) ----存放临时生成的排序条件
Declare @intCounts int ----要移动的记录数
Declare @BeginID int ----开始的ID
Declare @EndID int ----结束的ID
--------首先生成排序方法---------
if @Sort=0 --升序
begin
if not(@fldSort is null)
set @sqlSort = ' Order by ' + @fldSort
else
set @sqlSort = ' Order by ' + @ID
end
else --降序
begin
if not(@fldSort is null)
set @sqlSort = ' Order by ' + @fldSort + ' DESC'
else
set @sqlSort = ' Order by ' + @ID + ' DESC '
end
--------生成查询语句--------
--此处@strTmp为取得查询结果数量的语句
if @strCondition is null --没有设置显示条件
begin
set @sqlTmp = @fldName + ' From ' + @tblName
set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName
set @strID = ' From ' + @tblName
end
else
begin
set @sqlTmp = + @fldName + 'From ' + @tblName + ' where ' + @strCondition
/* set @sqlTmp = + @fldName + 'From ' + @tblName */
set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName + ' where ' + @strCondition
set @strID = ' From ' + @tblName + ' where ' + @strCondition
end
----取得查询结果总数量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
--取得分页总数
if @Counts <= @pageSize
set @pageCount = 1
else
set @pageCount = (@Counts / @pageSize) + 1
--计算要移动的记录数
if @page = 1
set @intCounts = @pageSize
else
begin
set @intCounts = (@page-1) * @pageSize + 1
end
-----取得分页后此页的第一条记录的ID
set @strID = 'select @BeginID=' + @ID + ' ' + @strID
set @intCounts = @intCounts - @pageSize + 1
set rowcount @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@BeginID out
-----取得分页后此页的最后一条记录的ID
set @intCounts = @intCounts + @pageSize - 1
print @intCounts
set rowcount @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@EndID out
------恢复系统设置-----
set rowcount 0
SET NOCOUNT OFF
------返回查询结果-----
if @strCondition is null
set @strTmp = 'select ' + @sqlTmp + ' where ' + @ID + ' between ' + str(@BeginID) + ' and ' + str(@EndID)
else
/* set @strTmp = 'select ' + @sqlTmp + ' where ' + @ID +' between ' + str(@BeginID) + ' and ' + str(@EndID) + ' and ' + @strCondition */
set @strTmp = 'select ' + @sqlTmp + ' and ' + @ID +' between ' + str(@BeginID) + ' and ' + str(@EndID)
/* set @strTmp = 'select ' + @sqlTmp + ' and (' + @ID +' between ' + str(@BeginID) + ' and ' + str(@EndID) + ') and ' + @strCondition */
/* set @strTmp = 'select * from tbproduct where (xid between 1 and 2) and xvisible = 1' */
if not(@sqlSort is null)
set @strTmp = @strTmp + @sqlSort
exec sp_executesql @strTmp
GO
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
B.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
--EXEC GetListByPage @Table='dbo.tb_Report',@Field='*',@Where='',@GroupBy='',@OrderBy='',@PrimaryKeyField='ReportId',@PageIndex=3,@PageSize=2,@IsCount=1
ALTER PROCEDURE [dbo].[GetListByPage](
@Table varchar(500), --表名
@Field varchar(500) = '*', --读取字段
@Where varchar(500) = NULL, --Where条件
@GroupBy varchar(500) = NULL, --分组
@OrderBy varchar(500)= NULL, --排序字段
@PrimaryKeyField varchar(50), --主键必需
@PageIndex int = 1, --开始页码
@PageSize int = 10, --页大小
@IsCount bit = 0 --返回记录总数
------------------------------------------------------------------------------------------------
--当@IsCount为1时,将同时返回2张表,表0为记录总数,表1为查询结果
------------------------------------------------------------------------------------------------
)
AS
BEGIN
------------------------------------------------------------------------------------------------
DECLARE @strWhere nvarchar(500) --Where 条件
DECLARE @strGroupBy nvarchar(500) --GroupBy 条件 --
DECLARE @strOrderBy nvarchar(500) --OrderBy 条件
DECLARE @strSql nvarchar(max) --Sql 语句
---------------------------------------------------------------------------------------
IF @Where IS NOT NULL AND @Where != '' --Where 条件
BEGIN
SET @strWhere = ' WHERE ' + @Where + ' '
END
ELSE
BEGIN
SET @strWhere = ''
END
----------------------------------------------------------------------------------------------------
IF @GroupBy IS NOT NULL AND @GroupBy != '' --GroupBy 条件
BEGIN
SET @strGroupBy = ' GROUP BY ' + @GroupBy + ' '
END
ELSE
BEGIN
SET @strGroupBy = ''
END
----------------------------------------------------------------------------------------------------
IF @OrderBy IS NULL OR @OrderBy = '' --OrderBy 条件
BEGIN
SET @strOrderBy = ' ORDER BY ' + @PrimaryKeyField + ' DESC'
END
ELSE
BEGIN
SET @strOrderBy = ' ORDER BY ' + @OrderBy
END
----------------------------------------------------------------------------------------------------
--计算总行数
IF @IsCount = 1
BEGIN
SET @strSql= 'SELECT Count (*) AS RecordCount FROM ' + @Table + @strWhere + @strGroupBy
EXEC sp_executesql @strSql
--RETURN
END
----------------------------------------------------------------------------------------------------
IF @PageIndex < 1 --第一页提高性能
BEGIN
SET @PageIndex = 1
END
IF @PageIndex = 1
BEGIN
SET @strSql = 'SELECT TOP ' + str(@PageSize) + ' ' + @Field + ' FROM ' + @Table +
@strWhere + @strGroupBy + @strOrderBy
PRINT @strSql
EXEC sp_executesql @strSql
RETURN
END
----------------------------------------------------------------------------------------------------
--其他页
DECLARE @STARTID nvarchar(50)
DECLARE @ENDID nvarchar(50)
SET @STARTID = convert(nvarchar(50),(@PageIndex - 1) * @PageSize + 1)
SET @ENDID = convert(nvarchar(50),@PageIndex * @PageSize)
SET @strSql = 'WITH MYTABLE AS (SELECT ROW_NUMBER() OVER (' + @strOrderBy + ')
AS RowNumber,' + @Field + ' FROM '+ @Table + @strWhere + @strGroupBy + ')
SELECT * FROM MYTABLE
WHERE RowNumber BETWEEN ' + @STARTID + ' AND ' + @ENDID
PRINT @strSql
EXECUTE sp_executesql @strSql
--------------------------------------------------------------------------------------------------
END
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
C.网上盛为流传的“千万级数据分页存储过程”
Create PROCEDURE CN5135_SP_Pagination
/*
***************************************************************
** 千万数量级分页存储过程 **
***************************************************************
参数说明:
1.Tables :表名称,视图
2.PrimaryKey :主关键字
3.Sort :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc
4.CurrentPage :当前页码
5.PageSize :分页尺寸
6.Filter :过滤语句,不带Where
7.Group :Group语句,不带Group By
效果演示:http://www.cn5135.com/_App/Enterprise/QueryResult.aspx
***************************************************************/
(
@Tables varchar(1000),
@PrimaryKey varchar(100),
@CurrentPage int,
@PageSize int,
@Filter varchar(1000),
@Fields varchar(1000) = '*',
@Sort varchar(200) = NULL,
@Group varchar(1000) = NULL
)
AS
/*默认排序*/
IF @Sort IS NULL or @Sort = ''
SET @Sort = @PrimaryKey
DECLARE @SortTable varchar(100)
DECLARE @SortName varchar(100)
DECLARE @strSortColumn varchar(200)
DECLARE @operator char(2)
DECLARE @type varchar(100)
DECLARE @prec int
/*设定排序语句.*/
IF CHARINDEX('DESC',@Sort)>0
BEGIN
SET @strSortColumn = REPLACE(@Sort, 'DESC', '')
SET @operator = '<='
END
ELSE
BEGIN
IF CHARINDEX('ASC', @Sort) = 0
SET @strSortColumn = REPLACE(@Sort, 'ASC', '')
SET @operator = '>='
END
IF CHARINDEX('.', @strSortColumn) > 0
BEGIN
SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))
END
ELSE
BEGIN
SET @SortTable = @Tables
SET @SortName = @strSortColumn
END
Select @type=t.name, @prec=c.prec
FROM sysobjects o
JOIN syscolumns c on o.id=c.id
JOIN systypes t on c.xusertype=t.xusertype
Where o.name = @SortTable AND c.name = @SortName
IF CHARINDEX('char', @type) > 0
SET @type = @type + '(' + CAST(@prec AS varchar) + ')'
DECLARE @strPageSize varchar(50)
DECLARE @strStartRow varchar(50)
DECLARE @strFilter varchar(1000)
DECLARE @strSimpleFilter varchar(1000)
DECLARE @strGroup varchar(1000)
/*默认当前页*/
IF @CurrentPage < 1
SET @CurrentPage = 1
/*设置分页参数.*/
SET @strPageSize = CAST(@PageSize AS varchar(50))
SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize + 1) AS varchar(50))
/*筛选以及分组语句.*/
IF @Filter IS NOT NULL AND @Filter != ''
BEGIN
SET @strFilter = ' Where ' + @Filter + ' '
SET @strSimpleFilter = ' AND ' + @Filter + ' '
END
ELSE
BEGIN
SET @strSimpleFilter = ''
SET @strFilter = ''
END
IF @Group IS NOT NULL AND @Group != ''
SET @strGroup = ' GROUP BY ' + @Group + ' '
ELSE
SET @strGroup = ''
/*执行查询语句*/
EXEC(
'
DECLARE @SortColumn ' + @type + '
SET ROWCOUNT ' + @strStartRow + '
Select @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' orDER BY ' + @Sort + '
SET ROWCOUNT ' + @strPageSize + '
Select ' + @Fields + ' FROM ' + @Tables + ' Where ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' orDER BY ' + @Sort + '
'
)
GO
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////