title: How to display a temp table where values are inserted dynamically
body: <p>Basically I have this TSQL which looks up a condition in all tables in a database and trying to insert the records into this temp table. I'm having difficulty to display this temp table #TEMP1 at the end of execution.</p>
  
  <pre><code>OPEN MyCursor;
  FETCH NEXT FROM MyCursor INTO @Table
     WHILE @@FETCH_STATUS = 0 BEGIN 
          set @SQL = N'IF EXISTS(select 1 from Prod.' + @Table + ' where usedDate IS NULL)' + 
   'select ''' + @Table + ''' as TableName,* INTO #TEMP1 from Prod.' + @Table + ' WHERE usedDate IS NULL '
  
   EXECUTE sp_executesql @SQL
   FETCH NEXT FROM MyCursor INTO @Table
  END;
  CLOSE MyCursor;
  DEALLOCATE MyCursor;
  </code></pre>