是这样,写一个SQL语句返回一个值。
设有表T1 ( f1, f2, f3)
     T2 ( f1, f2, f3)
     T3 ( f1, f2, f3)
select T1.f1+T2.f1/(T2.f3-T3.f1)+T2.f2/(T1.f3+T3.f2) from T1, T2, T3 
如果当(T2.f3-T3.f1)或(T1.f3+T3.f2)为0时,系统会报错而不会返回值。如何写sql语句,当(T2.f3-T3.f1)或(T1.f3+T3.f2)为0时返回0,而(T2.f3-T3.f1)或(T1.f3+T3.f2)为不为0时返回正常的计算结果??ps: select 后的T1.f1+T2.f1/(T2.f3-T3.f1)+T2.f2/(T1.f3+T3.f2)这部分表达式的内容是不确定的!有可能会有很多相关的计算!!

解决方案 »

  1.   

    select 
    case when tnum3=0 then 0 else (tnum1+tnum2)/tnum3 end,
    case when tnum2=0 then 0 else (tnum3+tnum4)/tnum2 end,
    from t1
      

  2.   

    先谢谢你们的回答,基本上与我想的差不多!但还有一点,
    ps: select 后的T1.f1+T2.f1/(T2.f3-T3.f1)+T2.f2/(T1.f3+T3.f2)这部分表达式的内容是不确定的!有可能会有很多相关的计算!!
    也就是说有可能有很多需要判断的,也就是说我们是不能把case when后的语句写成固定的内容,这部分应该是动态的![:)]是不是有与除0有关的什么函数呀,SP呀,月光宝盒什么的是我不知道的?
      

  3.   

    to suton!写函数是个好办法,您能否给个提示如何写这个函数,实际上偶也想写个函数,可是水平有限不知道如何写!!先谢过了!!:D
      

  4.   

    如果用MSSQL2000或者Oracle9i
    完全可以用Case语句,不需要大费周折了.
    写法参考
    hnhb(不死鸟) 的
      

  5.   

    select 
    case when tnum3=0 then 0 else (tnum1+tnum2)/tnum3 end,
    case when tnum2=0 then 0 else (tnum3+tnum4)/tnum2 end,
    from t1

    select (tnum1+tnum2)/tnum3,(tnum3+tnum4)/tnum2 
    from t1 where tnum3 <> 0 and tnum2 <> 0
    union all
    select 0,0
    from t1 where tnum3 * tnum2 = 0