title: SQL Server Temporary Table from Complex Stored Procedure
body: <p>I have to create a temp table (#temp) from a variable or stored procedure</p>
  
  <p>(i.e)</p>
  
  <p>My stored procedure contains </p>
  
  <p>......</p>
  
  <pre><code>set @sql='select ...'
  set @sql=@sql+'..join..'
  set @sql=@sql+'....join..'
  </code></pre>
  
  <p>when i execute it (i.e) <code>exec (@sql)</code> it  returns some rows,i want to store that rows
  in a temp table.How to acheieve it?</p>
  
  <p>i tried something like</p>
  
  <pre><code>select
         id,name,ward_no into,........ 
  #temp
  
  from
     ( 
       exec (@sql)
     )derived
  </code></pre>
  
  <h2>Update</h2>
  
  <p>I found the logic</p>
  
  <p>(i.e)</p>
  
  <pre><code>create table #temp(....)
  
  insert #temp exec(@sql) 
  
  select * from #temp
  </code></pre>