drop temp table 但已经在数据库中创建了 'There is already an object named '#temp'

drop temp table but already founded 'There is already an object named '#temp' in the database'

部分代码需要在两个临时 table 之间进行交换,当我删除一个 table 并重新使用它时,我不能

create table #temp (id int)
create table #swap (id int)

drop table #temp

select * into #temp from #swap

drop table #swap
drop table #temp

我收到这个错误

Msg 2714, Level 16, State 1, Line 6 There is already an object named '#temp' in the database.

稍微改变一下你的逻辑流程。如果重要的是当 INSERT 发生时 #temp 为空,这应该可以满足您的需要。

create table #temp (id int)
create table #swap (id int)

<Add loop logic here>

truncate table #temp

insert #temp(id)
select id from #swap

<Close out loop logic>

drop table #swap
drop table #temp

我还明确了列名。 SELECT * 是生产代码中等待发生的事故。