drop temp table但已经创建'数据库中已经有一个名为'#temp'的对象'

问题描述 投票:0回答:1

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

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

drop table #temp

select * into #temp from #swap

drop table #swap
drop table #temp

我收到此错误

消息2714,级别16,状态1,行6数据库中已存在名为“#temp”的对象。

sql-server-2012 temp-tables
1个回答
1
投票

只需改变一下你的逻辑流程。如果重要的是当#temp发生时INSERT是空的,这应该做你需要的。

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 *是一个等待在生产代码中发生的事故。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.