写了个分页的存储过程,其中定义了个输出参数@Count,为了将总页数通过计算带出来,但是这里总是返回空值,不知道哪里出的问题,高手们看看哦!!!
Code
create proc UserInfos_SelectAllPaged
@pageCount int, --每页显示的条数
@pageNow int, --当前页数
@tableName varchar(50), --表名
@primaryName varchar(50), --表主键的名字
@Count int output --返回总页数
as
declare @sql varchar(2000)
declare @sqlcount varchar(500)
declare @countTemp int
set @sql = 'select top ' + cast(@pageCount as varchar) + ' * from ' + @tableName
+ ' where ' + @primaryName + ' not in (select top ' + cast(@pageCount*(@pageNow-1) as varchar)
+ ' ' + @primaryName + ' from '+ @tableName+')'
exec(@sql)
set @sqlcount = 'select ' + cast(@countTemp as varchar)+'=count(*) from @tableName'
exec(@sqlcount)
set @Count = @countTemp / @pageCount + 1
问题补充:
alter proc UserInfos_SelectAllPaged
@pageCount int, --每页显示的条数
@pageNow int, --当前页数
@tableName varchar(50), --表名
@primaryName varchar(50), --表主键的名字
@Count int output --返回总记录数
as
declare @sql varchar(2000)
declare @countTemp int
set @countTemp=( select count(*) from @tableName)
set @Count =@countTemp
set @sql = 'select top ' + cast(@pageCount as varchar) + ' * from ' + @tableName+ ' where ' + @primaryName + ' not in (select top ' + cast(@pageCount*(@pageNow-1) as varchar) + ' ' + @primaryName + ' from '+ @tableName+')'
exec(@sql)
经过改进,改成这个样子,但是set @countTemp=( select count(*) from @tableName) 这一句里的@tableName总是报错说必须定义变量@tableName,什么问题?