MSSQL的里,我自己写的存储过程,但是执行老是有问题,求大神们帮我看看,感激不尽!
-分页查询的存储过程
create procedure pro_stu(
@pageIndex int,--页码
@pageSize int,--页面大小
@column nvarchar(128),--列名
@TableName nvarchar(128)--表名
)
as
declare @startRow int, @endRow int ,@sqlstr nvarchar(max)
set @startRow = (@pageIndex - 1) * @pageSize +1
set @endRow = @startRow + @pageSize -1
set @sqlstr='select * from (
select *, row_number() over (order by '+@column+' asc) as number from' + @tablename
+') as t
where t.number between'+ @startRow +'and'+ @endRow
exec (@sqlstr)
--执行语句
exec pro_stu 2,2,'id','TT'
错误提提
消息 245,级别 16,状态 1,过程 pro_stu,第 12 行
在将 nvarchar 值 'select * from (
select *, row_number() over (order by id asc) as number fromTT) as t
where t.number between' 转换成数据类型 int 时失败。
我也不知道是不是哪错了,望大神们帮我改正
------解决方案--------------------create procedure pro_stu(
@pageIndex int,--页码
@pageSize int,--页面大小
@column nvarchar(128),--列名
@TableName nvarchar(128)--表名
)
as
declare @startRow int, @endRow int ,@sqlstr nvarchar(max)
set @startRow = (@pageIndex - 1) * @pageSize +1
set @endRow = @startRow + @pageSize -1
set @sqlstr='select * from (
select *, row_number() over (order by '+@column+' asc) as number from' + @tablename
+') as t
where t.number between'+ Cast(@startRow as varchar(10)) +'and'+ Cast(@endRow as varchar(10))
print @sqlstr
exec (@sqlstr)
试试这个