if @type='all'
    select * into #a1 from table1
else
    select * into #a1 from table1 where id>100这明明不会同时执行的,为什么老提示“#a1”对象已经存在?

解决方案 »

  1.   

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

  2.   

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

  3.   

    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的机制的原因吧.
      

  4.   

    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
      

  5.   

    先drop table #al 一下.会死啊...
      

  6.   

    改改IF OBJECT_ID('Tempdb..#a1') IS NOT NULL
    DROP TABLE #a1
      select * into #a1 from table1 AND  @type='all'
    UNION all
      select *  from table1 where id>100 AND  @type<>'all'