...
select ID1
into #tmp
from Aif (select count(*) from #tmp)=0
begin
  if object_id('temp..#tmp') is not null
   drop table #tmp  select ID2
  into #tmp
  from A
end
...首先创建一个临时表#tmp,若表里没有数据,则把另外字段填入该表#tmp中,
报错提示存在#tmp,不知该怎么实现?

解决方案 »

  1.   

    需求修改如下:
    ...
    select identity(int 1,1), ID1
    into #tmp
    from Aif (select count(*) from #tmp)=0
    begin
      if object_id('temp..#tmp') is not null
       drop table #tmp  select identity(int 1,1),ID2
      into #tmp
      from A
    end
    ...首先创建一个临时表#tmp,若表里没有数据,则把另外字段填入该表#tmp中,
    报错提示存在#tmp,不知该怎么实现?
      

  2.   

    select identity(int 1,1),ID2
      into #tmp
    会产生新的表,肯定和原来表冲突。
      

  3.   

    select b=identity(int,1,1), a
    into #tmp
    from bb
    go
      if (select count(*) from #tmp)=0
    begin
      if object_id('tempdb..#tmp') is not null  select b=identity(int,1,1),a
      into #tmp
      from bb
    end
    else
    print 1
      

  4.   

    同一个数据库中的表名是不能重复的,而
    select ID1
    into #tmp
    from A
    每次都会产生新表#tmp.可以改为这样:
    insert into #tmp
    select id1
    from A