为什么我不能在 T-SQL 中重用临时表?

发布于 2024-10-03 04:01:31 字数 1058 浏览 4 评论 0原文

下面是一些示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长发绾君心 2024-10-10 04:01:31

SQL Server 查询优化器的更改

if object_id('tempdb..#TempList') is not null drop table #TempList
drop table #TempList

create table #TempList (

if object_id('tempdb..#TempList') is not null drop table #TempList
drop table #TempList

GO;

create table #TempList (

会让人感到困惑。

它看到您第二次创建同一个表。
请注意,表删除(以及创建)具有概率结果,
结果只有在运行时才知道
(不是基于语法控制或查询执行计划创建,即 SQL Server 术语中的“编译”)

Change

if object_id('tempdb..#TempList') is not null drop table #TempList
drop table #TempList

create table #TempList (

to

if object_id('tempdb..#TempList') is not null drop table #TempList
drop table #TempList

GO;

create table #TempList (

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)

流心雨 2024-10-10 04:01:31

对我来说似乎是一个解析器错误。我打赌它会看到两个创建表语句创建同一个表并抛出错误,无论 drop 语句如何。不过,用 go 语句将其分解效果很好。

create table #Temp (ID int)
insert into #Temp (ID)
select 1 union all select 2

select ID from #Temp

drop table #Temp
go
create table #Temp (ID int)

insert into #Temp (ID)
select 10 union all select 11

select ID from #Temp

drop table #Temp

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.

create table #Temp (ID int)
insert into #Temp (ID)
select 1 union all select 2

select ID from #Temp

drop table #Temp
go
create table #Temp (ID int)

insert into #Temp (ID)
select 10 union all select 11

select ID from #Temp

drop table #Temp
眼泪也成诗 2024-10-10 04:01:31

这是范围界定和#temp 表的问题。由于您可以与同一个命名的 #temp 表建立两个连接,因此元数据不会通过您指定的名称直接引用它。

该行为在 这篇博文相当彻底。

您需要使用 LIKE 比较运行不同的查询来删除 #temp 表:

IF EXISTS (
SELECT *
FROM sys.tables
WHERE name LIKE '#templist%')
DROP TABLE #templist
CREATE TABLE #templist...

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:

IF EXISTS (
SELECT *
FROM sys.tables
WHERE name LIKE '#templist%')
DROP TABLE #templist
CREATE TABLE #templist...
只怪假的太真实 2024-10-10 04:01:31

是的。使用完毕后将桌子扔掉。 :)

Yes. Drop the table when you are done using it. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文