为什么我不能在 T-SQL 中重用临时表?
下面是一些示例代码:
if object_id('tempdb..#TempList') is not null drop table #TempList
create table #TempList (
ID int,
Name varchar(20)
)
insert into #TempList values (1, 'Alpha')
insert into #TempList values (2, 'Beta')
insert into #TempList values (3, 'Gamma')
insert into #TempList values (4, 'Delta')
insert into #TempList values (5, 'Omega')
select * from #TempList
if object_id('tempdb..#TempList') is not null drop table #TempList
drop table #TempList
create table #TempList (
ID_New int,
AnotherID int,
Name_New varchar(40)
)
insert into #TempList values (100, 110, 'Orange')
insert into #TempList values (101, 111, 'Red')
insert into #TempList values (102, 112, 'Purple')
insert into #TempList values (103, 113, 'Blue')
insert into #TempList values (104, 114, 'Green')
select * from #TempList
这会产生以下错误:
Msg 2714, Level 16, State 1, Line 19
There is already an object named '#TempList' in the database.
我无法在同一 SQL 脚本中重复使用相同的临时表名称吗?有没有办法重新使用相同的临时表名称?
谢谢。
Here's some sample code:
if object_id('tempdb..#TempList') is not null drop table #TempList
create table #TempList (
ID int,
Name varchar(20)
)
insert into #TempList values (1, 'Alpha')
insert into #TempList values (2, 'Beta')
insert into #TempList values (3, 'Gamma')
insert into #TempList values (4, 'Delta')
insert into #TempList values (5, 'Omega')
select * from #TempList
if object_id('tempdb..#TempList') is not null drop table #TempList
drop table #TempList
create table #TempList (
ID_New int,
AnotherID int,
Name_New varchar(40)
)
insert into #TempList values (100, 110, 'Orange')
insert into #TempList values (101, 111, 'Red')
insert into #TempList values (102, 112, 'Purple')
insert into #TempList values (103, 113, 'Blue')
insert into #TempList values (104, 114, 'Green')
select * from #TempList
This yields the following error:
Msg 2714, Level 16, State 1, Line 19
There is already an object named '#TempList' in the database.
Am I not able to re-use the same temp table name within the same SQL script? Is there a way to re-use the same temp table name?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQL Server 查询优化器的更改
对
会让人感到困惑。
它看到您第二次创建同一个表。
请注意,表删除(以及创建)具有概率结果,
结果只有在运行时才知道
(不是基于语法控制或查询执行计划创建,即 SQL Server 术语中的“编译”)
Change
to
SQL Server query optimizer gets confused.
It sees that you create the same table second time.
Note that table drop (as well as create) has probabilistic outcome,
the result will be known only at run-time
(not upon syntax control or query exec plan creation aka "compilation" in jargon of SQL Server)
对我来说似乎是一个解析器错误。我打赌它会看到两个创建表语句创建同一个表并抛出错误,无论 drop 语句如何。不过,用 go 语句将其分解效果很好。
Seems like a parser error to me. I'm betting it sees the two create table statements creating the same table and throws the error, regardless of the drop statement. Breaking it up with a go statement works fine though.
这是范围界定和
#temp
表的问题。由于您可以与同一个命名的#temp
表建立两个连接,因此元数据不会通过您指定的名称直接引用它。该行为在 这篇博文相当彻底。
您需要使用
LIKE
比较运行不同的查询来删除#temp
表:It's an issue with scoping and
#temp
tables. Since you can have two connections with the same named#temp
table, the metadata doesn't directly refer to it by the name you specify.The behavior is explained in this blog post pretty thoroughly.
You need to run a different query using a
LIKE
comparison to remove the#temp
table:是的。使用完毕后将桌子扔掉。 :)
Yes. Drop the table when you are done using it. :)