当前位置: 代码迷 >> Sql Server >> 怎样写这样的函数?该如何解决
  详细解决方案

怎样写这样的函数?该如何解决

热度:34   发布时间:2016-04-27 17:02:58.0
怎样写这样的函数?
要写一个这样的函数,如果参数A小于10,返回3*A*1.2;
如果参数A大于等于10而小于25,返回4*A*1.2;
如果参数A大于等于25而小于35,返回5*A*1.2;
如果参数A大于等于35,返回6*A*1.2;
该怎样写?



------解决方案--------------------
select (case
when A <10 THEN 3*A*1.2
when A> =10 AND A <25 THEN 4*A*1.2
when A> =25 AND A <35 THEN 5*A*1.2
when A> =35 THEN 6*A*1.2
) from ...
------解决方案--------------------

create function fun(@A decimal(10, 2))
returns decimal(10, 2)
as
begin
declare @re decimal(10, 2)
if @A <10
set @[email protected]*3*1.2
else if @A> =10 and @A <25
set @[email protected]*4*1.2
else if @A> =25 and @A <35
set @[email protected]*5*1.2
else
set @[email protected]*6*1.2

return @re
end
  相关解决方案