declare @s varchar(8000)
set @s=''
select 
    @s=@s+','+a.ts_name+'|'+b.ts_name+'完成  =sum(case when type='+rtrim(a.ts_id)+' and subtype='+rtrim(b.ts_id)+' and ts_activeinactive=1 then 1 else 0 end)'
      
from  Selection_Issuetype a,Selection_subtype b where a.ts_id=b.type order by a.ts_id,b.ts_id
set @s='select '+stuff(@s,1,1,'')+' from usr_incidents'
exec(@s)declare @s varchar(8000)
set @s=''
select 
    a.ts_name+'|'+b.ts_name+'完成  =sum(case when type='+rtrim(a.ts_id)+' and subtype='+rtrim(b.ts_id)+' and ts_activeinactive=1 then 1 else 0 end)'
      
from  Selection_Issuetype a,Selection_subtype b where a.ts_id=b.type order by a.ts_id,b.ts_id
set @s='select '+stuff(@s,1,1,'')+' from usr_incidents'
exec(@s)

解决方案 »

  1.   

    用个print @s 打印出来看看
      

  2.   

    应该是b.ts_name中可能含有一些特殊字段所致在别名两边加[],改成如下:declare @s varchar(8000)
    set @s=''
    select 
        @s=@s+',['+a.ts_name+'|'+b.ts_name+'完成]  =sum(case when type='+rtrim(a.ts_id)+' and subtype='+rtrim(b.ts_id)+' and ts_activeinactive=1 then 1 else 0 end)'
          
    from  Selection_Issuetype a,Selection_subtype b where a.ts_id=b.type order by a.ts_id,b.ts_id
    set @s='select '+stuff(@s,1,1,'')+' from usr_incidents'
    exec(@s)