做成函数如何?
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)
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)
解决方案 »
- 多表关联update的问题
- 求救:如下的存储过程为什么在调用时总提示要输入@ID参数,能不能不输入
- 查询语句求助
- SQL 2005中报错创建 "DTS.pipeline.1" 的任务未注册,不能使用,请问如何解决?
- 急!救命!数据库被覆盖如何恢复?
- sql 时间截取
- 累计信息表 业务表 用英语怎么拼|? -_- 作为表名
- 对应关系表,并条件检索
- 高分求救SQL语句,急~~~
- 急,简单数据库表设计,解决马上给分
- 一个表有个自动增长的字段。把表中数据都删除了。怎么让再增长时从0开始?
- 哪位师兄给指些数据库基础的资料,什么联合 主键 外键 事务 我都部懂 只知道简单的SQL语句,分数部多,请别嫌弃,谢谢了。
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调用