简单的分页过程,确在第10,20,30....等页,不能显示,请求帮忙,谢谢

USE [factory]
GO
/****** Object:  StoredProcedure [dbo].[sp_AbiTableLoad]    Script Date: 10/18/2010 14:43:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_AbiTableLoad]
----------------------------------------------
--        单表多表分页存储过程    --
-- 1.支持单表多表分页查询     --
-- 2.支持排序        --
-- 3.支持表名重命名       --
-- 4.返回总页数        --
-- 5.返回总记录数       --
-- 6.带行编号        --
-- 缺陷:         --
-- 1.多表查询时,各表中各列名不许出现重名 --
-- Edit by Shardine 2007.1.19    --
----------------------------------------------
@strFields nvarchar(500),  --字段名
@strTableName nvarchar(500), --表名
@strWhere nvarchar(500),  --条件 无需加where
@strOrderBy nvarchar(200), --排序 必添 无需加order by
@PageSize int,    --分页大小
@CurrentPage int,   --当前页,1为起始页
@PageCount int output,  --返回总页数
@RecordCount int output  --返回记录总数
as
begin
declare @StartIndex int     --定义起始位置
set @StartIndex = (@currentPage - 1) * @PageSize + 1
declare @strSql1 nvarchar (800) --数据查询
declare @strSql2 nvarchar (1000) --统计记录总数
declare @ParmDefinition nvarchar (800)
set @ParmDefinition = N'@tmp int output'
set @strSql1 = N'select row_number() over (order by ' + @strOrderBy + ' ) as RowID, '
set @strSql2 = 'select @tmp = count(*) '
if @strFields <> ''
 set @strSql1 = @strSql1 + @strFields
else
 set @strSql1 = @strSql1 + ' * '
if @strTableName <> ''
begin
 set @strSql1 = @strSql1 + ' from ' + @strTableName
 set @strSql2 = @strSql2 + ' from ' + @strTableName
end
if @strWhere <> ''
begin
 set @strSql1 = @strSql1 + ' where ' + @strWhere
 set @strSql2 = @strSql2 + ' where ' + @strWhere
end
exec sp_executesql @strSql2,@ParmDefinition,@tmp = @RecordCount output  --执行统计记录总数SQL语句

if @RecordCount % @PageSize = 0  --计算总页数
 set @PageCount = @RecordCount / @PageSize
else
 set @PageCount = @RecordCount / @PageSize + 1
set @strSql1 = 'with TempTable as ( ' + @strSql1 + ' ) select * from TempTable where RowID between '
  + Convert(nvarchar(50),@StartIndex) + ' and ' + Convert(nvarchar(50),@StartIndex + @PageSize - 1)
exec(@strSql1)
end

posted @ 2010-10-18 14:48  tangself  阅读(404)  评论(0编辑  收藏  举报