title: Converting dynamic sql query's COUNT result to INT
body: <p>I am trying to search within the values (table names) returned from a query to check if there is a record and some values in that record are null. If they are, then I want to insert the table's name to a temporary table. I get an error:</p>
  
  <pre><code>Conversion failed when converting the varchar value 'count(*) 
  FROM  step_inusd_20130618 WHERE jobDateClosed IS NULL' to data type int.
  </code></pre>
  
  <p>This is the query:</p>
  
  <pre><code>  DECLARE @table_name VARCHAR(150)
    DECLARE @sql VARCHAR(1000)
    DECLARE @test int
  
    SELECT @table_name =  tableName FROM  #temp WHERE id = @count
  
     SET @sql = 'SELECT * FROM  ' + @table_name + ' WHERE jobDateClosed IS NULL'
  --ERROR is below:
  select @test =   'count(*) FROM  ' + @table_name + ' WHERE jobDateClosed IS NULL'
     --PRINT @sql
    -- EXEC(@sql)
     IF @test &gt; 0 
     BEGIN 
      INSERT INTO #temp2 (tablename) VALUES ( @table_name);
     END
    SET @count = @count + 1
  </code></pre>
  
  <p>Any ideas how to convert the result of the count into an integer?</p>