利用两次Top操作,结合Order By实现分页
Code
USE [Northwind]
GO
/****** 对象: StoredProcedure [dbo].[SeparatePage] 脚本日期: 06/09/2009 15:43:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SeparatePage]
-- Add the parameters for the stored procedure here
@SelectList nvarchar(100),--选取字段列表
@TableSource nvarchar(50),--数据源名称表名或视图名称
@SearchCondition nvarchar(100),--where筛选条件
@OrderExpression nvarchar(50),--排序 必须指定一个排序字段
@pageindex int,--页索引 从1开始
@pagesize int,--每页记录数
@TotalRecorder int output,--输出总记录数
@TotalPage int output--输出总页数
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
declare @sql nvarchar(4000)--用来拼接SQL语句
declare @reverseOrder nvarchar(50)--反向排序变量
declare @selectTop int --临时Top变量
--获取总记录数
declare @tempSql nvarchar(100)
set @tempSql = 'select @TotalRecorder=count(*) from '+@TableSource
exec sp_executesql @tempSql,N'@TotalRecorder int output',@TotalRecorder output
--计算总页数
set @TotalPage = Ceiling(@TotalRecorder/Convert(float,@pagesize))
--设置Top变量
if @pagesize*@pageindex > @TotalRecorder
set @selectTop = @TotalRecorder
else
set @selectTop = @pagesize*@pageindex
--设置反向排序变量
if CharIndex('ASC',@OrderExpression) <> 0
set @reverseOrder = replace(@OrderExpression,'ASC','DESC')
else
set @reverseOrder = replace(@OrderExpression,'DESC','ASC')
set @sql = 'SELECT '+@SelectList+' FROM (SELECT TOP '+convert(varchar(10),@selectTop-(@pagesize*(@pageindex-1)))+
@SelectList+' FROM (SELECT TOP '+Convert(varchar(10),@selectTop)+ ' '+@SelectList+
' FROM '+@TableSource+' ORDER BY '+@OrderExpression +') as b ORDER BY '+@reverseOrder+') as a order by '+@OrderExpression
exec(@sql)
END
USE [Northwind]
GO
/****** 对象: StoredProcedure [dbo].[SeparatePage] 脚本日期: 06/09/2009 15:43:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SeparatePage]
-- Add the parameters for the stored procedure here
@SelectList nvarchar(100),--选取字段列表
@TableSource nvarchar(50),--数据源名称表名或视图名称
@SearchCondition nvarchar(100),--where筛选条件
@OrderExpression nvarchar(50),--排序 必须指定一个排序字段
@pageindex int,--页索引 从1开始
@pagesize int,--每页记录数
@TotalRecorder int output,--输出总记录数
@TotalPage int output--输出总页数
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
declare @sql nvarchar(4000)--用来拼接SQL语句
declare @reverseOrder nvarchar(50)--反向排序变量
declare @selectTop int --临时Top变量
--获取总记录数
declare @tempSql nvarchar(100)
set @tempSql = 'select @TotalRecorder=count(*) from '+@TableSource
exec sp_executesql @tempSql,N'@TotalRecorder int output',@TotalRecorder output
--计算总页数
set @TotalPage = Ceiling(@TotalRecorder/Convert(float,@pagesize))
--设置Top变量
if @pagesize*@pageindex > @TotalRecorder
set @selectTop = @TotalRecorder
else
set @selectTop = @pagesize*@pageindex
--设置反向排序变量
if CharIndex('ASC',@OrderExpression) <> 0
set @reverseOrder = replace(@OrderExpression,'ASC','DESC')
else
set @reverseOrder = replace(@OrderExpression,'DESC','ASC')
set @sql = 'SELECT '+@SelectList+' FROM (SELECT TOP '+convert(varchar(10),@selectTop-(@pagesize*(@pageindex-1)))+
@SelectList+' FROM (SELECT TOP '+Convert(varchar(10),@selectTop)+ ' '+@SelectList+
' FROM '+@TableSource+' ORDER BY '+@OrderExpression +') as b ORDER BY '+@reverseOrder+') as a order by '+@OrderExpression
exec(@sql)
END