存贮过程中有如下片段:if @a=1 
   select 1,1,1 into #temp
else if @a=2 
   select 2,2,2 into #temp
else 
   sleect 2,2 into #temp上述语句在运行时会报错, 说#temp已在数据库中存在, 当然我可以生成#temp进行插入操作, 但问题是这条路就行不通吗?

解决方案 »

  1.   


    先用if 语句判断下表是存在
    存在删除表
    然后
    if @a=1 
       select 1,1,1 into #temp
    else if @a=2 
       select 2,2,2 into #temp
    else 
       sleect 2,2 into #temp
      

  2.   

    if  object_id('tempdb..#temp') is not null 
    begin 
    Drop Table #Temp
    end 
    if @a=1 
       select 1,1,1 into #temp
    else if @a=2 
       select 2,2,2 into #temp
    else 
       sleect 2,2 into #temp
      

  3.   

    这样是行不通的!
    解决方法是:就如你说的,先建立一个表,该表包含最多的列,
    如: #temp(a1 int,a2 int,a3 int)
    判断后插入数据,从第一列开始填充数据!
      

  4.   

    楼上的方法, 表变量不能用于select select_list Into table操作,T-SQL的帮助里写明,不过我还是准备改造使用表变量