做成函数如何?
create function  dbo.Water_price (@ton float)
returns smallmoney
as 
begin
declare @rmb smallmoney
if @ton>20
 begin
  set @rmb=(@ton-20)*1.5+10*1+10*0.5  
 end
else
 begin 
 if @ton>10 
  set @rmb=(@ton-10)*1+10*0.5 
  else 
  set @rmb=@ton*0.5
 end
return @rmb
end
go
到时候只要这样就可以了:
select dbo.Water_price (128)

解决方案 »

  1.   

    用量下限,用量上限,单价1,单价2
    0         10         0      0.5
    10        20         0.5    1
    20        99999      1      1.5只有用表才灵活,如果到时候要改,只需让用户维护此表即可。create function test (@a numeric(10,2)     -- @a 为实际用量
    returns numeric(10,2)
    as
    begin
       declare @b numeric(10,2)
       select @b = 用量下限 * 单价1 + (用量上限-@a) * 单价2
           from yourTable where @a >= 用量下限 and @b < 且量上限
       Return @b
    end调用