首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-04-27 
关于sql中case判断的使用SQL codecreate function SXTEST(@type int,@first float,@second float)returns

关于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 

热点排行
Bad Request.