2000下ROW_NUMBER()不支持,下面存储过程,谁帮转换下,谢谢
ALTER PROCEDURE [dbo].[p_Paging_RowCount]
(
@TableName varchar(100), --表名
@Fields varchar(5000) , --字段名(全部字段为*)
@OrderField varchar(5000), --排序字段(必须!支持多字段)
@sqlWhere varchar(5000),--条件语句(不用加where)
@pageSize int, --每页多少条记录
@pageIndex int , --指定当前为第几页
@TotalPage int output, --返回总页数
@TotalRecord int output -- 返回总记录数
)
AS
begin
Begin Tran --开始事务
Declare @sql nvarchar(4000);
--Declare @totalRecord int;
--计算总记录数
if (@SqlWhere='' or @sqlWhere is NULL)
begin
set @sql = 'select @TotalRecord = count(*) from ' + @TableName
end
else
begin
if(@SqlWhere like '%group by%')
set @sql = 'select @TotalRecord = count(*) from (select a.ID,a.MingCheng,a.ShangYeQuan,a.XingXiangPic,a.FangJianNum,a.ZhuCeTime,a.Dizhi,a.DianHua,a.Jiaotong,a.Xingji,a.Remark,b.MID from ' + @TableName + ' where ' + @sqlWhere+') as A'
else
set @sql = 'select @TotalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
end
EXEC sp_executesql @sql,N'@TotalRecord int OUTPUT',@TotalRecord OUTPUT--计算总记录数
--计算总页数
select @TotalPage=CEILING((@TotalRecord+0.0)/@PageSize)
if (@SqlWhere='' or @sqlWhere is NULL)
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
else
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere
--处理页数超出范围情况
if @PageIndex<=0
Set @pageIndex = 1
if @pageIndex>@TotalPage
Set @pageIndex = @TotalPage
--处理开始点和结束点
Declare @StartRecord int
Declare @EndRecord int
set @StartRecord = (@pageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @pageSize - 1
--继续合成sql语句
set @Sql = @Sql + ') as myTable where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
Exec(@Sql)
--select @sql
---------------------------------------------------
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit Tran
--Return @totalRecord ---返回记录总数
End
end
[解决办法]
帮你修改还不如直接给你一个现成可用的.参考吧
CREATE PROC proc_pageview
@tbname sysname, --要分页显示的表名
@FieldKey nvarchar(1000), --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
@PageCurrent int=1, --要显示的页码
@PageSize int=10, --每页的大小(记录数)
@FieldShow nvarchar(1000)='', --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
@FieldOrder nvarchar(1000)='', --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC
@Where varchar(1000)='', --查询条件
@RecordCount int OUTPUT --总页数
AS
SET NOCOUNT ON
--检查对象是否有效
IF OBJECT_ID(@tbname) IS NULL
BEGIN
RAISERROR(N'对象"%s"不存在',1,16,@tbname)
RETURN
END
IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
BEGIN
RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)
RETURN
END
--分页字段检查
IF ISNULL(@FieldKey,N'')=''
BEGIN
RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
RETURN
END
--其他参数检查及规范
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
IF ISNULL(@FieldOrder,N'')=N''
SET @FieldOrder=N''
ELSE
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
IF ISNULL(@Where,N'')=N''
SET @Where=N''
ELSE
SET @Where=N'WHERE ('+@Where+N')'
--如果@PageCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@PageCount赋值)
IF @RecordCount IS NULL
BEGIN
DECLARE @sql nvarchar(4000)
SET @sql=N'SELECT @RecordCount=COUNT(*)'
+N' FROM '+@tbname
+N' '+@Where
EXEC sp_executesql @sql,N'@RecordCount int OUTPUT',@RecordCount OUTPUT
END
--计算分页显示的TOPN值
DECLARE @TopN varchar(20),@TopN1 varchar(20)
SELECT @TopN=@PageSize,
@TopN1=(@PageCurrent-1)*@PageSize
--第一页直接显示
IF @PageCurrent=1
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' '+@Where
+N' '+@FieldOrder)
ELSE
BEGIN
--处理别名
IF @FieldShow=N'*'
SET @FieldShow=N'a.*'
--生成主键(惟一键)处理条件
DECLARE @Where1 nvarchar(4000),@Where2 nvarchar(4000),
@s nvarchar(1000),@Field sysname
SELECT @Where1=N'',@Where2=N'',@s=@FieldKey
WHILE CHARINDEX(N',',@s)>0
SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),
@s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
@Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,
@Where2=@Where2+N' AND b.'+@Field+N' IS NULL',
@Where=REPLACE(@Where,@Field,N'a.'+@Field),
@FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),
@FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)
SELECT @Where=REPLACE(@Where,@s,N'a.'+@s),
@FieldOrder=REPLACE(@FieldOrder,@s,N'a.'+@s),
@FieldShow=REPLACE(@FieldShow,@s,N'a.'+@s),
@Where1=STUFF(@Where1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),
@Where2=CASE
WHEN @Where='' THEN N'WHERE ('
ELSE @Where+N' AND ('
END+N'b.'+@s+N' IS NULL'+@Where2+N')'
--执行查询
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' a LEFT JOIN(SELECT TOP '+@TopN1
+N' '+@FieldKey
+N' FROM '+@tbname
+N' a '+@Where
+N' '+@FieldOrder
+N')b ON '+@Where1
+N' '+@Where2
+N' '+@FieldOrder)
END
GO
CREATE proc pro_page
@RecordCount int output, --查询到的记录总数,输出参数
@QueryStr nvarchar(1000)='table1',--表名、视图名、查询语句
@PageSize int=20, --每页的大小(行数)
@PageCurrent int=0, --要显示的页 从0开始
@FdShow nvarchar (2000)='*', --要显示的字段列表
@IdentityStr nvarchar (100)='id', --主键
@WhereStr nvarchar (2000)='1=1',
@FdOrder nvarchar(100)='desc' --排序 只能取desc或者asc
as
set nocount on
declare
@sql nvarchar(2000)
if @WhereStr = '' begin
set @WhereStr = '1=1'
end
declare @tsql nvarchar(200)
set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr + ' where ' + @WhereStr
exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount output
if @PageCurrent = 0 begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' ' + @FdOrder
end
else begin
if upper(@FdOrder) = 'DESC' begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '< ( select min(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' desc) as t) order by ' + @IdentityStr + ' desc'
end
else begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' asc) as t) order by ' + @IdentityStr + ' asc'
end
end
--print @sql
execute(@sql)
-------------------------------------------------------------------------------
调用方法:(注意输出参数的写法)
DECLARE @i INT
execute pro_page @QueryStr='t_college',
@IdentityStr='clgId',@PageSize=3,@PageCurrent=0,@FdOrder='asc',@RecordCount=@i output;
select @i;
这样调用结果就包括了,返回结果集,和输出参数(查询到的记录总数)
alter procedure [dbo].[p_Paging_RowCount]
(@TableName varchar(100), --表名
@Fields varchar(5000) , --字段名(全部字段为*)
@OrderField varchar(5000), --排序字段(必须!支持多字段)
@sqlWhere varchar(5000),--条件语句(不用加where)
@pageSize int, --每页多少条记录
@pageIndex int , --指定当前为第几页
@TotalPage int output, --返回总页数
@TotalRecord int output -- 返回总记录数
)
AS
begin
Declare @sql nvarchar(4000);
--Declare @totalRecord int;
--计算总记录数
if (@SqlWhere='' or @sqlWhere is NULL)
begin
set @sql = 'select @TotalRecord = count(*) from ' + @TableName
end
else
begin
if(@SqlWhere like '%group by%')
set @sql='select @TotalRecord = count(*) from (select a.ID,a.MingCheng,a.ShangYeQuan,a.XingXiangPic,a.FangJianNum,a.ZhuCeTime,a.Dizhi,a.DianHua,a.Jiaotong,a.Xingji,a.Remark,b.MID from ' + @TableName + ' where ' + @sqlWhere+') as A'
else
set @sql='select @TotalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
end
EXEC sp_executesql @sql,N'@TotalRecord int OUTPUT',@TotalRecord output --计算总记录数
--计算总页数
select @TotalPage=CEILING((@TotalRecord+0.0)/@PageSize)
if (@SqlWhere='' or @sqlWhere is NULL)
set @sql = 'select identity(int,1,1) ''rowId'',' + @Fields + ' into #t from ' + @TableName + ' order by '+@OrderField+';'
else
set @sql = 'select identity(int,1,1) ''rowId'',' + @Fields + ' into #t from ' + @TableName + ' where ' + @SqlWhere + ' order by '+@OrderField+';'
--处理页数超出范围情况
if @PageIndex<=0
Set @pageIndex = 1
if @pageIndex>@TotalPage
Set @pageIndex = @TotalPage
--处理开始点和结束点
Declare @StartRecord int
Declare @EndRecord int
set @StartRecord = (@pageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @pageSize - 1
--继续合成sql语句
set @Sql = @Sql + 'select * from #t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
exec(@Sql)
end