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
@queryis null, then@tablenamemust be null