By trying this
use master
go
select * into #TempTable from sys.all_views
select * from #TempTable
drop table #TempTable
exec('
select * into #TempTable2 from sys.all_views
')
/* This will give error: */
select * from #TempTable2
I realized that #TempTable2 is not accessible… so using the select into #TempTable syntax is used inside an EXEC statement means the table is auto destroyed as the exec statement is completed?
Yes.
You can see this with
You can use a
##globaltemporary table if you want to be able to access it later. e.g.