当前位置:首页 > PHP教程 > PHP总结归纳

OracleDecode()函数和CASE语句的比较

oracle decode()函数和case语句都是我们经常用到的,那么它们的区别在哪里呢?下面就为您详细介绍 oracle decode()函数和case语句的区别,供您参考。 首先,举2个简单的例子,简单对比一下这2者的区别。 1.case语句: 以下是代码片段: selectcasesign(5-5) w

oracle decode()函数和case语句都是我们经常用到的,那么它们的区别在哪里呢?下面就为您详细介绍

oracle decode()函数和case语句的区别,供您参考。

  首先,举2个简单的例子,简单对比一下这2者的区别。

  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’)

fromdual

  后台实现:

  以下是代码片段:

  switch ( sign(5 – 5) )

{

case 1 : 'is positive'; break;

case 2 : 'is negative'; break;

default : ‘is zero’

}

  在上面的例子中,2者似乎都可以实现。但是,在碰到非凡的问题时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更为简洁。

.syntaxhighlighter{padding-top:20px;padding-bottom:20px;}

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!