Here is what I have so far. I want to store the query result into an int variable but I get null so far. After execute, both @query and @countrow are both null. Any suggestion would be great.
SET @query = N'select @countrow=count(*) from ' + @tablename
EXECUTE sp_executesql @query
Take a look at SQL Fiddle