title: Unable to copy data to temporary table using select statement in sql - server 2008
body: <p>I am copying the data to temporary table using select statement which produces the result of pivoting.</p>
  
  <p>My procedure is:  </p>
  
  <pre><code>alter procedure proc11 AS
  
  create Table #Temp (Software varchar(max), Ver varchar(max))
  
  declare @cols varchar(max)
  declare @colTabl varchar(max)
  declare @alter varchar(max)
  
  set @cols=STUFF((select distinct  ',[' + Ltrim(rtrim(YEAR(Dt))) +']' from temp FOR XML PATH('')),1,1,'');
  
  set @colTabl=STUFF((select distinct  '[' + Ltrim(rtrim(YEAR(Dt))) +'] int,' from temp FOR XML PATH('')),1,0,'');
  
  set @colTabl= LEFT(@colTabl,len(@colTabl)-1);
  
  set @alter = 'alter table #Temp add  '+ @colTabl;
  
  exec(@alter)
  
  EXEC('select * into #Temp from 
  (select YEAR(Dt)[year],Software,Ver from temp)T
  Pivot(count([year]) for [year] in ('+@cols+'))PVT')
  </code></pre>
  
  <p>But the statement <code>select * from #Temp</code> returns no record.</p>
  
  <p>How do I copy these records. </p>
  
  <p>Please help. Thanks in advance.</p>