存储过程中出现的错误。

newabby 2004-01-07 10:40:52
declare @SQL varchar(2000)
DECLARE @tblName varchar(50)
DECLARE @totalRec int


set @SQL='select @totalRec=count(*) from '+@tblName
exec (@SQL)

以上语句,执行时出现以下错误,我该怎么解决,如何才能获得查询到的记录数。

服务器: 消息 137,级别 15,状态 1,行 1
必须声明变量 '@totalRec'。
...全文
82 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-01-07
  • 打赏
  • 举报
回复
declare @SQL Nvarchar(4000) --注意这里的定义
DECLARE @tblName varchar(50)
DECLARE @totalRec int

set @SQL='select @totalRec=count(*) from '+@tblName
exec sp_executesql @SQL,N'@totalRec int output',@totalRec output
newabby 2004-01-07
  • 打赏
  • 举报
回复
现在出现以下错误:

服务器: 消息 214,级别 16,状态 2,过程 sp_executesql,行 24
过程需要参数 '@statement' 为 'ntext/nchar/nvarchar' 类型。
txlicenhe 2004-01-07
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2364/2364046.xml?temp=.829693
[交流]动态SQL语句

3: 输出参数
eg:
declare @num,
@sqls
set @sqls='select count(*) from '+@servername+'.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中

declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
txlicenhe 2004-01-07
  • 打赏
  • 举报
回复
declare @SQL nvarchar(2000)
DECLARE @tblName varchar(50)
DECLARE @totalRec int


set @SQL='select @totalRec=count(*) from '+@tblName
exec sp_executesql @sql,N'@totalRec int output',@totalRec output
select @totalRec
viptiger 2004-01-07
  • 打赏
  • 举报
回复
declare @SQL nvarchar(2000)
DECLARE @tblName nvarchar(50)
DECLARE @totalRec int


set @SQL=N'select @totalRec=count(*) from '+@tblName
EXEC sp_executesql @SQL,
N'@totalRec int',
@totalRec = @totalRec


34,590

社区成员

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

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