title: dynamically creating the #temp table and then used to insert the data
body: <p>I am importing an intermediate result of query into a temp table for further use, so I used a #temp table to maintain the same schema as</p>
  
  <pre><code>select * into # temp from schema.tableName where 1&lt;&gt;1;
  
  insert into # temp from exec(table)
  </code></pre>
  
  <p>While I am doing this statement as a variable to pass the different tableName its not working</p>
  
  <pre><code>SET @TEMPSCHEMA = 'SELECT * INTO #temp FROM ' + @PKSchema + '.dbo.' + @PKTableName + ' WHERE 1&lt;&gt;1;'
  
  exec(@TEMPSCHEMA)
  
  
  INSERT INTO #temp
  
  EXEC ( @SELECTSTATEMENT
  
  )
  </code></pre>
  
  <p>however after exec statement it is not taking the values to the temp table.</p>
  
  <p>it is showing the invalid object id #temp</p>