Home > Database > Mysql Tutorial > Oracle Decode()函数和CASE语句的比较

Oracle Decode()函数和CASE语句的比较

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:19:58
Original
1077 people have browsed it

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 
WHEN X-FIELD 
WHEN X-FIELD 
ELSE ‘UNBEKNOWN’END

FROM DUAL

  因此,个人认为,,CASE语句在处理类似问题就显得非常灵活。当只是需要匹配少量数值时,用Decode更为简洁。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template