日期:2014-05-19  浏览次数:20488 次

如何把返回结果保存在一个变量
Declare   @tcount   int
set   @tcount=1
Declare   @sql   varchar(100)
set   @sql= 'select   count(*)   from   Product '
Execute   @tcount=@sql
这样提示找不到存储过程  
而我这里不需要用存储过程来实现
请大家帮助,谢了

------解决方案--------------------
Declare @tcount int
Declare @sql nvarchar(100)
set @sql=N 'select @tcount = count(*) from Product '
Execute sp_executesql @sql,N '@tcount int OUTPUT ',@tcount OUTPUT

SELECT @tcount
------解决方案--------------------
----例子---

use pubs

Declare @tcount int
Declare @sql nvarchar(100)
set @sql=N 'select @tcount = count(*) from jobs '
Execute sp_executesql @sql,N '@tcount int OUTPUT ',@tcount OUTPUT

SELECT @tcount
------解决方案--------------------
Declare @tcount int
set @tcount=1
Declare @sql nvarchar(100)
set @sql= 'select count(*) from Product '
exec sp_executesql @sql,N '@tcount int output ',@tcount output
print @tcount