An exec(<string>) executes the <string> in a its own batch, therefor the @CNT variable is out of scope. There are two ways to do what you are trying to do.First, use sp_executesql. Take a look at Books on Line for the specifics, but . . .create table sekar ( col1 int )insert sekar values (1)insert sekar values (2)insert sekar values (3)godeclare @tth varchar(50), @sql nvarchar(1000), @theCount intset @tth = '<databasename>.<owner>.sekar'select @sql = N'select @cnt = count(47) from ' + @tthexec master..sp_executesql @sql, N'@cnt INT OUT', @theCount OUTselect @theCountgodrop table sekargo
Second, you can run the execute into a temp table . . .create table sekar ( col1 int )insert sekar values (1)insert sekar values (2)insert sekar values (3)gocreate table #count ( thecount int)declare @tth varchar(50), @sql varchar(1000)set @tth = '<databasename>.<owner>.sekar'select @sql = 'select count(47) from ' + @tthinsert #countexec(@sql)select thecount from #countgodrop table sekardrop table #countgo
Hope that helps.<O>