+ -
当前位置:首页 → 问答吧 → 为什么老提示“#a1”对象已经存在?

为什么老提示“#a1”对象已经存在?

时间:2011-12-02

来源:互联网

if @type='all'
  select * into #a1 from table1
else
  select * into #a1 from table1 where id>100

这明明不会同时执行的,为什么老提示“#a1”对象已经存在?

作者: glassy2000   发布时间: 2011-12-02

不是这里的问题,你先 drop table #a1 试试.

作者: xuam   发布时间: 2011-12-02

如果是在存储过程中的话,同一个存储过程不允许有两次 into #同名,尽管你放在不同的if else 块中.
解决办法是,先创建临时表 #,再在if else 里用 insert 语句向其中插入值.

作者: qianjin036a   发布时间: 2011-12-02

SQL code


Select * Into #a1 from table1 where 1<>1
if @type='all'
  insert #a1 select *  from table1
else
  insert #a1 select *  from table1 where id>100

--好像SQL的机制的原因吧.

作者: mustudent   发布时间: 2011-12-02

SQL code
if object_id('tempdb..#a1') is not null 
drop table #a1
go
if @type='all'
  select * into #a1 from table1
else
  select * into #a1 from table1 where id>100

作者: fredrickhu   发布时间: 2011-12-02

引用 2 楼 qianjin036a 的回复:
如果是在存储过程中的话,同一个存储过程不允许有两次 into #同名,尽管你放在不同的if else 块中.
解决办法是,先创建临时表 #,再在if else 里用 insert 语句向其中插入值.

1

作者: pengxuan   发布时间: 2011-12-02

对,我就是在存储过程中的

看来比较麻烦

作者: glassy2000   发布时间: 2011-12-02

先drop 掉。

作者: jinfengyiye   发布时间: 2011-12-02

热门下载

更多