I'm building a procedure into SQL Server, and for that I need to receive a table name as a variable, create a temporary table based on the table requested, search some substrings on each column of the given table, remove then, and return the result to the user. For that, I'm first tried to do that:
declare @TableName varchar(max)
select @TableName = 'xxxxxx'
select * into #temp from @TableName
select * from #temp
From this code, I've got the error:
Must declare the table variable "@TableName"
To use a table variable, I need to know the structure of said table beforehand, which I don't.
If I build the statement and execute it inside an EXEC()
function, then the scope changes, and the temporary table will not be available for the outer process.
Is there a way to use the variable directly, or to declare this table variable dynamically?