case
             when  d.ActionNo=0 then 'a'
             when  d.ActionNo=2 then 'b'
             when  d.ActionNo=1 then 'c'
             when  d.ActionNo=4 then 'd'
             when  d.ActionNo=6 then 'e'
             when  d.ActionNo=7 then 'f'
             when  e.LoanNm is null and d.ActionNo=8 then 'g'
             when  e.LoanNm is not null and d.ActionNo=8  then 'h'
               .
..........N个条件
             else ''
end这种判断会反复调用,请问怎么写成一个函数呢?

解决方案 »

  1.   

    就那样写啊
    create  function fun(@id int)
    returns varchar(max)
    as
    begin
    return case @id when 1 then 'a' when 2 then 'b' when 4 then 'c' when 3 then 'd' else 'e' end
    end
      

  2.   

    use tempdb
    goCreate function fn_Num(@ActionNo smallint,@LoanNm int)
    returns varchar(2)
    as
    begin
    return (case when  @ActionNo=0 then 'a'
                 when  @ActionNo=2 then 'b'
                 when  @ActionNo=1 then 'c'
                 when  @ActionNo=4 then 'd'
                 when  @ActionNo=6 then 'e'
                 when  @ActionNo=7 then 'f'
                 when  @LoanNm is null and @ActionNo=8 then 'g'
                 when  @LoanNm is not null and @ActionNo=8  then 'h'
                 else '' end)
     END
    goselect dbo.fn_Num(d.ActionNo,LoanNm) from d,e
      

  3.   

    @ActionNo smallint,@LoanNm int-- 类型同字段类型要相同
      

  4.   

    那你应该要建立一个表
     ActionNo value
    0 'a'
    1 'b'
    ...
      

  5.   

    放到FUNCTION,在给一个变量当参数就行了。