title: Using values from Temp table in Dynamic SQL
body: <p>I have a temp table which has few records . I need to include them in my Dynamic SQL.I am trying to do something like this </p>
  
  <pre><code>SET @SQL=@SQL + 'Select '+ @fields + ' ID from dbo.EmployeePension()' 
  SET @SQL = @SQL + ' WHERE EmployeeID IN'
  SET @SQL= @SQL + '(Select EmployeeID from' + #temp4 + ')'
  EXEC @SQL
  </code></pre>
  
  <p>where #temp4 is my temporary table.
  I am actually pulling @fields from a separate table and then retrieving the same columns from the function.</p>
  
  <p>Can someone help me on how to include the temp table values in dynamic SQL.
  I am expecting a dynamic SQL like</p>
  
  <p>Select name,age,sex from dbo.EmployeePension() where EmployeeID IN(Select EmployeeID in (100,101,102));</p>
  
  <p>where (100,101,102) are values in temp table and name,age,sex are values in @fields.</p>