服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > 数据库技术 > Oracle技术 > Oracle开发 > 查看文档

oracle-decode()函数和case语句有哪些不同

【赛迪网-it技术报道】oracle-decode()函数和case语句的区别:

具体示例如下:

1.case语句:

select case sign(5 - 5)

when 1 then 'is positive'

when -1 then 'is negative'

else 'is zero' end

from dual;

后台实现:

if (sign(5 ? 5) = 1) {

'is positive';

} else if (sign(5 ? 5) = 2 ) {

'is negative';

}else {

‘is zero’

}

2. decode函数:

select decode(sign(5 ? 5), 1, 'is positive', -1, 'is negative', ‘is zero’)

from dual

后台实现:

switch ( sign(5 ? 5) )

{

case 1 : 'is positive'; break;

case 2 : 'is negative'; break;

default : ‘is zero’

}

虽然在上面的示例中,两者看似都可以实现。但在遇到特殊情况时,decode()的实现就相对复杂得多了。

例如:

select case x-field

when x-field < 40 then ‘x-field < 40’

when x-field < 50 then ‘x-field < 50’

when x-field < 60 then ‘x-field < 60’

else ‘unbeknown’end

from dual

相对而言,case语句在处理相似问题就显得比较简捷灵活。另外,当需要匹配少量数值时,选用decode会更加方便一些。

扫描关注微信公众号