读书人

关于sql中case判断的使用解决思路

发布时间: 2012-04-27 11:57:44 作者: rapoo

关于sql中case判断的使用

SQL code
create function SXTEST(@type int,@first float,@second float)returns floatasbegindeclare @result floatif(@type=1)--"+"begin    set @result=@first+@secondendif(@type=2)--"-"begin    set @result=@first-@secondendreturn @resultend


以上代码不用if,用case when 如何实现??

[解决办法]
SQL code
create function SXTEST(@type int,@first float,@second float)returns floatasbegindeclare @result floatselect @result = case @type when 1 then @first+@second when 2 then @first - @second endend
[解决办法]
SQL code
create function SXTEST(@type int,@first float,@second float)returns floatasbegindeclare @result floatSELECT @result= CASE WHEN @type=1 THEN @first+@second                      WHEN @type=0 THEN @first-@second                     ELSE 0 ENDreturn @resultend 

读书人网 >SQL Server

热点推荐