execute for count inside cursor, sql server
<p>I have a requirement where i have to find out the number of records for each table names which are already listed in a master table.</p>

<p>the master table looks like:
RecordId, TableName, RowCount
initially all the rows for RowCOunt set to 0.</p>

<p>what i did was, created a cursor</p>

<pre><code>declare @tName nvarchar(max)
declare @query nvarchar(max)
declare @count int = 0
declare curCount cursor for select TableName from MasterTable
open curCount
fetch next from curCount into @tName
while @@fetch_status=0
begin
    set @tName = @tName
    set @query = N'select count(ID) from ['+@tName+']';
    set @count = execute @query
    update @tempTbl set RecordCount = @count where TableName  = @tName
    print @query
    fetch next from curCount into @tName
end
close curCount
deallocate curCount
</code></pre>

<p>it gives me error every time at this point
set @count = execute @query, error is: Incorrect syntax near the keyword 'exec'.</p>

<p>i have tried sp_executesql @query as well... it also gives me error, error is Incorrect syntax near '@query'.</p>

<p>Please help me to fix this.</p>