I am creating a stored procedure which has to create a table which name depends on input variable. For temporary results I have declared a table variable:
declare @tableA TABLE(
column1,
column2,
..
)
The output table name depends on the user’s input so I have declared another variable
declare @tableB varchar = ...
In the end the temporary results have to be stored into table which name is @tableB so in the stored procedure I have tried to write the following statement:
declare @sql varchar(max)
set @sql = 'SELECT * INTO ' + @tableB + ' FROM @tableA'
exec(@sql)
which is not correct. Does anyone know how to insert values from table variable into table which name is variable?
You can’t use table variables here, try to use temporary tables.
create table #tableA( …
For more information: link text