把 DROP TABLE 这一句提到最前面。 临时表在整个会话中都存在,你只要第二次执行这段语句,#temp01就是已存在的状态啊。
先创建表,然后用insert into #tmp select ……
if 1>2 begin IF Object_id('tempdb..#temp01') IS NOT NULL DROP TABLE #temp01 select 1 into #temp01 end else begin IF Object_id('tempdb..#temp01') IS NOT NULL DROP TABLE #temp01 select 2 into #temp01 end
按照你的逻辑运行是可能没有问题,问题在SQL编译的时候却认为临时表已经存在,即一次会话中只能SELECT INTO 一次的方式建立临时表 你可以用普通表替代,使用完主动删除
(
.......
)
if
insert #test
else
insert #test
临时表在整个会话中都存在,你只要第二次执行这段语句,#temp01就是已存在的状态啊。
begin
IF Object_id('tempdb..#temp01') IS NOT NULL DROP TABLE #temp01
select 1 into #temp01
end
else
begin
IF Object_id('tempdb..#temp01') IS NOT NULL DROP TABLE #temp01
select 2 into #temp01
end
你可以用普通表替代,使用完主动删除