title: sql server - fill results from executed query string in a temp table dynamically
body: <p>I'm writing a stored procedure. I have a string which contains an sql query. For example:</p>
  
  <pre><code>DECLARE @sql nvarchar(max)
  SET @sql = (N'SELECT pkOrderID FROM Orders') 
  </code></pre>
  
  <p>(Just to note: this isn't what the select statement looks like. This is just an example of what I mean) I then want to execute the string and put the result in a temporary table E.g. #tempTable. I know EXEC(@sql) exists but not sure if it will do me any good in this situation. The other twist is that I do not know the names of all the columns in the returned @sql so the  temp table #tempTable needs to be created dyanmically based off the return from @sql. Thanks for any help. </p>