admin

从exec(@sql)返回值

sql

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

这是我的查询:

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

阅读 240

收藏
2021-05-10

共1个答案

admin

一方面,您可以使用sp_executesql:

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

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

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