danjia=cast((case when shuliang=0 then 0 else zongjia/shuliang*1.0 end) as decimal(10,2))

解决方案 »

  1.   

    四捨五入
    danjia=round((case when shuliang=0 then 0 else zongjia/shuliang*1.0 end),2)  
    非四捨五入
    danjia=round((case when shuliang=0 then 0 else zongjia/shuliang*1.0 end),2,1)
      

  2.   

    create table test (
    总价 decimal(10,2) not null,
    数量 int not null default(0),
    单价 as Cast(Case when 数量 = 0 then 0 else 总价/数量 end  as decimal(10,2))
    )
    insert into test values (55.22,0)
    insert into test values (55.22,2)
    select * from test