我正在尝试使用变量在Microsoft SQL Server 2014上创建临时表。
DECLARE @TableName VARCHAR(50)
SET @TableName = 'MyShema.MyTable'
EXEC('SELECT * INTO #temp FROM ' + @TableName)
SELECT * FROM #temp
但是,最后一行SELECT * FROM #temp
给出Invalid object name '#temp.
任何人都可以告诉我如何修复它?谢谢!
答案 0 :(得分:1)
答案 1 :(得分:1)
DECLARE @TableName1 VARCHAR(50)
SET @TableName1 = 'dbo.encounter'
EXEC('SELECT * INTO ##temp FROM ' + @TableName1)
SELECT * FROM ##temp
答案 2 :(得分:0)
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'SELECT * INTO #temp FROM from MyShema.MyTable ; SELECT * FROM #tmpA';
EXEC sp_executesql @sql