if (len(@currentMonth)>5 and LEN(@unit)>0)
    select * into #tmpAnalytics from #tmpSrc where 1=1 and [发生时间] in (@currentMonth,@lastMonth) and [受理大单位] in(@unit)
else if len(@unit)>0 
    select * into #tmpAnalytics from #tmpSrc where 1=1 and [受理大单位] in(@unit)
else if len(@currentMonth)>5 
    select * into #tmpAnalytics from #tmpSrc where 1=1 and [发生时间] in (@currentMonth,@lastMonth) 

解决方案 »

  1.   

    肯定不行,数据库预编译的时候就会出错,#tmpAnalytics已经生成,第二个还怎么生成。
    如果需要你得定义一个表变量
    declare @tb table (..)
    insert into @tb
     select * from #tmpSrc
      

  2.   

    如果 @unit 是一个由逗号分隔的字符串,则
    in (@unit)
    不可以直接执行.
    要用动态语句拼接.
      

  3.   

    顶..而且..还是create吧..create table #tmpAnalytics(......)if (len(@currentMonth)>5 and LEN(@unit)>0)
        select *  from #tmpSrc where 1=1 and [发生时间] in (@currentMonth,@lastMonth) and [受理大单位] in(@unit)
    else if len(@unit)>0 
        select *  from #tmpSrc where 1=1 and [受理大单位] in(@unit)
    else if len(@currentMonth)>5 
        select *  from #tmpSrc where 1=1 and [发生时间] in (@currentMonth,@lastMonth)