当前位置: 代码迷 >> Sql Server >> 怎么把返回结果保存在一个变量
  详细解决方案

怎么把返回结果保存在一个变量

热度:6   发布时间:2016-04-27 20:15:20.0
如何把返回结果保存在一个变量
Declare   @tcount   int
set   @tcount=1
Declare   @sql   varchar(100)
set   @sql= 'select   count(*)   from   Product '
Execute   @[email protected]
这样提示找不到存储过程  
而我这里不需要用存储过程来实现
请大家帮助,谢了

------解决方案--------------------
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
  相关解决方案