简体   繁体   English

选择进入#temp表时出现错误

[英]Select into #temp table got an error

I have complicated store procedure and get the data into the #temp table that is worked fine. 我有复杂的存储过程,并将数据放入工作正常的#temp表中。 However when I filter the data from this temp table then I got the error, 'Invalid object name '#temp_2'. 但是,当我从该临时表中筛选数据时,出现错误“无效的对象名称'#temp_2'。 After the data in #temp_2 table, I also need to open into cursor. 在#temp_2表中的数据之后,我还需要打开游标。 Would someone tell me how to solve it. 有人可以告诉我如何解决。

There is my script: 有我的脚本:

DECLARE @sql varchar(500)
IF @strLocations=''
SET @sql='SELECT *  into #temp_2 from #temp_1' 
ELSE
SET @sql='SELECT * into #temp_2 from #temp_1 t  where t.Location_id  in ( ' + @strLocations + ')'
 EXEC (@sql)

select * from #temp_2

EXEC creates a new session to execute the statements. EXEC创建一个新的会话来执行语句。 When the session ends, any local temporary tables created in it are destroyed (ie dropped). 会话结束时,在其中创建的所有本地临时表都将被销毁(即删除)。 You will have to create the temporary table before your EXEC statement. 您将必须在EXEC语句之前创建临时表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM