sql server 2000中如何分页?
谢谢!sqlserver2005中有row_number函数,很好分页,可是2000怎么分页呢?...
谢谢!
sql server 2005中有row_number函数,很好分页,可是2000怎么分页呢? 展开
sql server 2005中有row_number函数,很好分页,可是2000怎么分页呢? 展开
1个回答
展开全部
/* Function: SuperPaging
* Description:
* 超强通用分页存储过程
* Example:
* SuperPaging @TableName='表名',@Orderfld='排序列名'
*/
CREATE PROCEDURE SupesoftPage
(
@TableName nvarchar(50), -- 表名
@ReturnFields nvarchar(2000) = '*', -- 需要返回的列
@PageSize int = 10, -- 每页记录数
@PageIndex int = 1, -- 当前页码
@Where nvarchar(2000) = '', -- 查询条件
@Orderfld nvarchar(2000), -- 排序字段名 最好为唯一主键
@OrderType int = 1 -- 排序类型 1:降序 其它为升序
)
AS
DECLARE @TotalRecord int
DECLARE @TotalPage int
DECLARE @CurrentPageSize int
DECLARE @TotalRecordForPageIndex int
DECLARE @OrderBy nvarchar(255)
DECLARE @CutOrderBy nvarchar(255)
if @OrderType = 1
BEGIN
set @OrderBy = ' Order by ' + REPLACE(@Orderfld,',',' desc,') + ' desc '
set @CutOrderBy = ' Order by '+ REPLACE(@Orderfld,',',' asc,') + ' asc '
END
else
BEGIN
set @OrderBy = ' Order by ' + REPLACE(@Orderfld,',',' asc,') + ' asc '
set @CutOrderBy = ' Order by '+ REPLACE(@Orderfld,',',' desc,') + ' desc '
END
-- 记录总数
declare @countSql nvarchar(4000)
set @countSql='SELECT @TotalRecord=Count(*) From '+@TableName+' '+@Where
execute sp_executesql @countSql,N'@TotalRecord int out',@TotalRecord out
SET @TotalPage=(@TotalRecord-1)/@PageSize+1
SET @CurrentPageSize=@PageSize
IF(@TotalPage=@PageIndex)
BEGIN
SET @CurrentPageSize=@TotalRecord%@PageSize
IF(@CurrentPageSize=0)
SET @CurrentPageSize=@PageSize
END
-- 返回记录
set @TotalRecordForPageIndex=@PageIndex*@PageSize
exec('SELECT * FROM
(SELECT TOP '+@CurrentPageSize+' * FROM
(SELECT TOP '+@TotalRecordForPageIndex+' '+@ReturnFields+'
FROM '+@TableName+' '+@Where+' '+@OrderBy+') TB2
'+@CutOrderBy+') TB3
'+@OrderBy)
-- 返回总页数和总记录数
SELECT @TotalPage as PageCount,@TotalRecord as RecordCount
GO
* Description:
* 超强通用分页存储过程
* Example:
* SuperPaging @TableName='表名',@Orderfld='排序列名'
*/
CREATE PROCEDURE SupesoftPage
(
@TableName nvarchar(50), -- 表名
@ReturnFields nvarchar(2000) = '*', -- 需要返回的列
@PageSize int = 10, -- 每页记录数
@PageIndex int = 1, -- 当前页码
@Where nvarchar(2000) = '', -- 查询条件
@Orderfld nvarchar(2000), -- 排序字段名 最好为唯一主键
@OrderType int = 1 -- 排序类型 1:降序 其它为升序
)
AS
DECLARE @TotalRecord int
DECLARE @TotalPage int
DECLARE @CurrentPageSize int
DECLARE @TotalRecordForPageIndex int
DECLARE @OrderBy nvarchar(255)
DECLARE @CutOrderBy nvarchar(255)
if @OrderType = 1
BEGIN
set @OrderBy = ' Order by ' + REPLACE(@Orderfld,',',' desc,') + ' desc '
set @CutOrderBy = ' Order by '+ REPLACE(@Orderfld,',',' asc,') + ' asc '
END
else
BEGIN
set @OrderBy = ' Order by ' + REPLACE(@Orderfld,',',' asc,') + ' asc '
set @CutOrderBy = ' Order by '+ REPLACE(@Orderfld,',',' desc,') + ' desc '
END
-- 记录总数
declare @countSql nvarchar(4000)
set @countSql='SELECT @TotalRecord=Count(*) From '+@TableName+' '+@Where
execute sp_executesql @countSql,N'@TotalRecord int out',@TotalRecord out
SET @TotalPage=(@TotalRecord-1)/@PageSize+1
SET @CurrentPageSize=@PageSize
IF(@TotalPage=@PageIndex)
BEGIN
SET @CurrentPageSize=@TotalRecord%@PageSize
IF(@CurrentPageSize=0)
SET @CurrentPageSize=@PageSize
END
-- 返回记录
set @TotalRecordForPageIndex=@PageIndex*@PageSize
exec('SELECT * FROM
(SELECT TOP '+@CurrentPageSize+' * FROM
(SELECT TOP '+@TotalRecordForPageIndex+' '+@ReturnFields+'
FROM '+@TableName+' '+@Where+' '+@OrderBy+') TB2
'+@CutOrderBy+') TB3
'+@OrderBy)
-- 返回总页数和总记录数
SELECT @TotalPage as PageCount,@TotalRecord as RecordCount
GO
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询