从 exec(@sql) 返回值 [英] Return value from exec(@sql)

查看:52
本文介绍了从 exec(@sql) 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Exec(@sql) 获取值并分配给 @Rowcount(int)

I want get the value from Exec(@sql) and assign to @Rowcount(int)

这是我的查询:

'SET @RowCount = (select count(*) 
                    FROM dbo.Comm_Services 
                   WHERE CompanyId = '+cast(@CompanyId as char)+' and '+@condition+')'

推荐答案

一方面你可以使用 sp_executesql:

On the one hand you could use sp_executesql:

exec sp_executesql N'select @rowcount=count(*) from anytable', 
                    N'@rowcount int output', @rowcount output;

另一方面,您可以使用临时表:

On the other hand you could use a temporary table:

declare @result table ([rowcount] int);
insert into @result ([rowcount])
exec (N'select count(*) from anytable');
declare @rowcount int = (select top (1) [rowcount] from @result);

这篇关于从 exec(@sql) 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆