I am trying to create a temp table on Microsoft SQL Server 2014 using a variable.
DECLARE @TableName VARCHAR(50)
SET @TableName = 'MyShema.MyTable'
EXEC('SELECT * INTO #temp FROM ' + @TableName)
SELECT * FROM #temp
However, the last line, SELECT * FROM #temp gives Invalid object name '#temp. Can anyone please tell me how I can fix it? Thank you!
@TableNameand create a temp table one by one.