if not exists(select name from tempdb.sys.objects where name like '%#outtable%')
        begin
          if(select count(*) from CODE_Pcode_create where orderno=@ordernum and isout='10') !=0
             begin
            select itemtype as '类型',prats as '产品名称',count(*) as '已出库' into #outtable from CODE_Pcode_create where orderno=@ordernum and isout='10'  group by itemtype,parts
             end
           else
           
            begin
             create table #outtable(类型 varchar(300),产品名称 varchar(300),已出库 int)
             insert into  #outtable(类型,产品名称,已出库) select distinct 产品名称,类型,0 from CODE_Pcode_create where orderno=@ordernum  
             end
          end
          else
             drop table #outtable总是提示#outable已存在

解决方案 »

  1.   

    create table #outtable(类型 varchar(300),产品名称 varchar(300),已出库 int)
    这行删除
      

  2.   

    创建#outtable之前要判断他是否存在。
      

  3.   

    你前面已经有
    select itemtype as '类型',prats as '产品名称',count(*) as '已出库' into #outtable
    那么这个时候#outtable就已经生成了
      

  4.   

    使用临时表之前,首先判断:
    IF (OBJECT_ID('#xxx') IS NOT NULL
    DROP TABLE #xxxCREATE TABLE #xxx (...)