Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to capture result

Author  Topic 

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2011-03-10 : 10:28:06
how can i get the result of the: exec(@SQL) stored into @qty?

basically i need to check how many records are in these tables, problem is the tablename needs to be dynamic.

ex:

create table [empty] (zip varchar(5))
create table [full] (zip varchar(5))
insert into [full] values ('12345')
insert into [full] values ('12345')
insert into [full] values ('12345')

declare @table1 varchar(30) = '[empty]'
declare @table2 varchar(30) = '[full]'
declare @qty int
declare @sql varchar(4000)

set @sql = 'select count(*) from ' + @table1; exec (@sql)
set @sql = 'select count(*) from ' + @table2; exec (@sql)

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-03-10 : 10:48:10
Read about sp_executesql in SQL Server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -