Home > OS >  Weird session scope reverse is possible in SQL
Weird session scope reverse is possible in SQL

Time:11-04

How can I insert data from a #temp table into a permanent table which is passed as varchar variable?

I am asking the opposite transfer of:

Declare @anytable varchar(99)='perma'
--insertexec
Insert into #temptablecreatedalready
Exec('select * from '  @anytable)

CodePudding user response:

Instead of adding the select alone in the query string, try the whole insert.

Declare @anytable = 'perma'
EXEC(
'Insert into '   @anytable   '
select * from #temptablecreatedalready')

Make sure that the #temptablecreatedalready table is created in the current session before the query string is executed.

  • Related