首页 新闻 会员 周边

临时表重复创建问题

0
悬赏园豆:50 [已关闭问题]

if (@bigtype = 1)
  begin
   select * into #temptable from a  

end
  else if (@bigtype = 2)
  begin
   select * into #temptable from a

  end

报#temptable重复,如何解决,先判断有表再删除似乎不起作用

问题补充: 难道就没有一个高手吗 再次提问,有没有能解答的
冷于冰的主页 冷于冰 | 初学一级 | 园豆:0
提问于:2010-02-02 11:50
< >
分享
其他回答(3)
0

if object_id('tempdb..#tempTable') is not null
Begin
    drop table #tempTable
End

 

试一下,加个这个判断

persialee | 园豆:3217 (老鸟四级) | 2010-02-02 12:00
这个我试过,似乎不行。
支持(0) 反对(0) 冷于冰 | 园豆:0 (初学一级) | 2010-02-02 12:59
0

用变量表 DECLARE @T TABLE(ID INT)

Leox | 园豆:461 (菜鸟二级) | 2010-02-02 16:42
此表具体什么样
支持(0) 反对(0) 冷于冰 | 园豆:0 (初学一级) | 2010-02-03 11:26
0

你可以试试这个:先创建#temptable

 

if (@bigtype = 1)
begin
insert into #temptable select a
end
else if (@bigtype = 2)
begin
insert into #temptable select a
end

另外,判断临时表是否存在

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

 

邀月 | 园豆:25475 (高人七级) | 2010-02-02 17:07
这个方法我知道,但我不想这样做,因为表参数太多,难道select into就不能解决吗
支持(0) 反对(0) 冷于冰 | 园豆:0 (初学一级) | 2010-02-02 20:16
0

暂时做了一些研究,还没有深入,得出的情况是这样

declare @bigtype int
set @bigtype = 1
begin tran
if (@bigtype = 1)
  begin
   select * into #tempTable1 from ErrorLog
   print '1'
  end
else if (@bigtype = 2)
  begin
   select * into #tempTable2 from ErrorLog
   print '2'
  end
commit tran

 

如果分支中临时表命名一样,就会报临时对象已经存在,同时下面的语句都会报没有对象,或者没有权限

select * from tempdb.sys.objects where object_id = object_id('tempdb..#tempTable1')

drop table #tempTable1

但是如果分支中使用两个不同的名字,就一切正常,可以查询的到,也可以删除

机制今天太忙,还没有来得及看

看能否解决楼主的问题

Alfred2006 | 园豆:255 (菜鸟二级) | 2010-02-04 17:10
我问的就是这样的问题,希望有人从理论上给我解答
支持(0) 反对(0) 冷于冰 | 园豆:0 (初学一级) | 2010-02-24 19:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册