You can store the data in Global temporary table (##) with Select * into approach and to store in #temp table you have to create the table first which I am aware of while using dynamic sql But you can certainly do that in run time but still you may need some physical table to access it.
create table testtmp (id int, namen varchar(15))
--inserting the data into physical table
insert into testtmp (id, namen)
select 1 as ID, 'XYZ' as namen union all
select 2 as ID, 'ABC' as namen union all
select 3 as ID, 'DIG' as namen
create table #temp (ID int)
declare @sql nvarchar(max) = 'select ID from testtmp'
insert into #temp exec sp_executesql @sql
select * from #temp
Gives you this output:
ID
1
2
3
With global temporary table you can do it easily and you don't have to create any tables, you can specify column names if you would like to.
declare @sql nvarchar(max) = 'select * into ##Gloabltmptest from testtmp'
exec sp_executesql @sql
select * from ##Gloabltmptest
Output:
ID namen
1 XYZ
2 ABC
3 DIG
Added table variable as well, similar to #temp tables.
declare @table table (IDtab int, nametab varchar(15))
declare @sql nvarchar(max) = 'select * from testtmp'
insert into @table exec sp_executesql @sql
select * from @table