select
    result = case len(a.result) 
                when 0 then '全部为空' 
                else stuff(a.result,1,1,'')+'字段有值'
             end 
from
    (select 
         max(case when 字段1 is not null then ',1' else '' end) + 
         max(case when 字段2 is not null then ',2' else '' end) + 
         max(case when 字段3 is not null then ',3' else '' end) + 
         max(case when 字段4 is not null then ',4' else '' end) + 
         max(case when 字段5 is not null then ',5' else '' end) as result
     from
         表) a

解决方案 »

  1.   

    存储过程不是纯sql语句吗?
    楼主指的纯sql语句是?
      

  2.   

    create table #T(c1 int,c2 int,c3 int,c4 int,c5 int)
    insert into #T select NULL,NULL,NULL,NULL,NULLselect
        result = case len(a.result) 
                    when 0 then '全部为空' 
                    else stuff(a.result,1,1,'')+'字段有值' 
                 end
    from
        (select 
             max(case when c1 is not null then ',1' else '' end) + 
             max(case when c2 is not null then ',2' else '' end) + 
             max(case when c3 is not null then ',3' else '' end) + 
             max(case when c4 is not null then ',4' else '' end) + 
             max(case when c5 is not null then ',5' else '' end) as result
         from
             #T) a--输出结果
    /*
    result
    ---------------
    全部为空
    */
    insert into #T select 1,NULL,NULL,NULL,NULL
    insert into #T select NULL,2,NULL,NULL,NULLselect
        result = case len(a.result) 
                    when 0 then '全部为空' 
                    else stuff(a.result,1,1,'')+'字段有值' 
                 end
    from
        (select 
             max(case when c1 is not null then ',1' else '' end) + 
             max(case when c2 is not null then ',2' else '' end) + 
             max(case when c3 is not null then ',3' else '' end) + 
             max(case when c4 is not null then ',4' else '' end) + 
             max(case when c5 is not null then ',5' else '' end) as result
         from
             #T) a--输出结果
    /*
    result
    ---------------
    1,2字段有值
    */insert into #T select NULL,2,3,NULL,5select
        result = case len(a.result) 
                    when 0 then '全部为空' 
                    else stuff(a.result,1,1,'')+'字段有值' 
                 end
    from
        (select 
             max(case when c1 is not null then ',1' else '' end) + 
             max(case when c2 is not null then ',2' else '' end) + 
             max(case when c3 is not null then ',3' else '' end) + 
             max(case when c4 is not null then ',4' else '' end) + 
             max(case when c5 is not null then ',5' else '' end) as result
         from
             #T) a  --输出结果
    /*
    result
    ---------------
    1,2,3,5字段有值
    */