sql求助

sxiaohui8709253 2010-03-19 12:56:52
ALTER PROCEDURE [dbo].[procSortPage]
(
@sqlString varchar(4000), --查询数据的语句
@PageSize int= 10, --页尺寸
@PageIndex int= 1, --页码
@PageCount int=0 output --总页数
)
AS
declare @RowCount int --总记录数
declare @strSelRowCount varchar(4000) --用来查询总记录数
set @strSelRowCount='select @RowCount= count(*) from ( '+@sqlString+' ) t'
exec (@strSelRowCount)
set @PageCount=@RowCount/@PageSize+1

执行的时候
declare @pagecount int;
exec procSortPage 'select * from test',2,2,@pagecount output

会报
消息 137,级别 15,状态 1,第 1 行
必须声明标量变量 "@RowCount"。
我上面定义了"@RowCount
请F姐姐 77哥 树哥。。众高手帮我看看,谢谢
...全文
177 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxiaohui8709253 2010-03-19
  • 打赏
  • 举报
回复
恩 多谢各位大师指点
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[procSortPage]
(
@sqlString varchar(4000), --查询数据的语句
@PageSize int= 10, --页尺寸
@PageIndex int= 1, --页码
@PageCount int=0 output --总页数
)
AS
declare @RowCount int --总记录数
declare @strSelRowCount nvarchar(4000) --该语句用来查询总记录数
set @strSelRowCount=N'select @RowCount= count(*) from ( '+@sqlString+' ) t'+' '
execute sp_executesql @strSelRowCount, N'@RowCount int output',@RowCount output
set @PageCount=@RowCount/@PageSize+1 --这里不严密 没判断刚好整出的情况
print @PageCount

这样可以了 原来@strSelRowCount 这个数据类型要nvarchar才可以 结贴了 ,谢谢大家
sql_sf 2010-03-19
  • 打赏
  • 举报
回复
ALTER PROCEDURE [dbo].[procSortPage]
(
@sqlString varchar(4000), --查询数据的语句
@PageSize int= 10, --页尺寸
@PageIndex int= 1, --页码
@PageCount int=0 output --总页数
)
AS
declare @RowCount int --总记录数
declare @strSelRowCount varchar(4000) --用来查询总记录数
set @strSelRowCount='select @RowCount= count(*) from ( '+@sqlString+' ) t'
sp_executesql @strSelRowCount,N'@RowCount output',@RowCount output
set @PageCount=@RowCount/@PageSize+1
go
declare @pagecount int;
exec procSortPage 'select * from test',2,2,@pagecount output
select @pagecount
sxiaohui8709253 2010-03-19
  • 打赏
  • 举报
回复
或者我想把执行select count(*) from ( select * from test ) t的结果给@RowCount的 怎么写,谢谢了
sxiaohui8709253 2010-03-19
  • 打赏
  • 举报
回复
print @strSelRowCount
的结果是set @RowCount=( select count(*) from ( select * from test ) t)

就是这句话用exec (@strSelRowCount)执行的时候
老报消息 137,级别 15,状态 1,第 1 行
必须声明标量变量 "@RowCount"

是exec (@strSelRowCount) 就执行不了啊
sxiaohui8709253 2010-03-19
  • 打赏
  • 举报
回复
恩 我去看看
--小F-- 2010-03-19
  • 打赏
  • 举报
回复
declare @pagecount int;
exec procSortPage 'select * from test',2,2,@pagecount output


这里输出参数 用SP_EXECUTESQL 执行
SQL77 2010-03-19
  • 打赏
  • 举报
回复
先PRINT 出来看看@SQL语句,

用SP_EXECUTESQL 执行

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧