title: Execute sp_executeSql for select...into #table but Can't Select out Temp Table Data
body: <p>Was trying to select...into a temp Table #TempTable in sp_Executedsql.
  Not its successfully inserted or not but there Messages there written 
  (359 row(s) affected) that mean successful inserted?
  Script below</p>
  
  <pre><code>DECLARE @Sql NVARCHAR(MAX);
  SET @Sql = 'select distinct Coloum1,Coloum2 into #TempTable 
              from SPCTable with(nolock)
              where Convert(varchar(10), Date_Tm, 120) Between @Date_From And @Date_To';
  
  SET @Sql = 'DECLARE @Date_From VARCHAR(10);
              DECLARE @Date_To VARCHAR(10);
              SET @Date_From = '''+CONVERT(VARCHAR(10),DATEADD(d,DATEDIFF(d,0,GETDATE()),0)-1,120)+''';
              SET @Date_To = '''+CONVERT(VARCHAR(10),DATEADD(d,DATEDIFF(d,0,GETDATE()),0)-1,120)+''';
              '+ @Sql;
  
  EXECUTE sp_executesql @Sql;
  </code></pre>
  
  <p>After executed,its return me on messages (359 row(s) affected).
  Next when trying to select out the data from #TempTable.</p>
  
  <pre><code>Select * From #TempTable;
  </code></pre>
  
  <p>Its return me:</p>
  
  <pre><code>Msg 208, Level 16, State 0, Line 2
  Invalid object name '#TempTable'.
  </code></pre>
  
  <p>Suspected its working only the 'select' section only. The insert is not working.
  how fix it?</p>