本文章简单的介绍了oracle中的decode()函数的用法及用实例说明了此函数在实际应用中的用法。
本文章简单的介绍了oracle中的decode()函数的用法及用实例说明了此函数在实际应用中的用法。
在oracle/ plsql的,decode函数有一个if - then - else语句的功能。
decode函数的语法是:
decode( expression , search , result [, search , result]... [, default] )
expression值进行比较。
search 是对表达相比的价值。
result是返回的值,如果表达式等于搜索。
default 是可选的。如果没有找到匹配,解码将返回默认值。如果省略了默认,然后解码语句将返回null(如果没有找到匹配)。
撤消修改
applies to:
oracle 9i, oracle 10g, oracle 11g
实例
select supplier_name,
decode(supplier_id, 10000, 'ibm',
10001, 'microsoft',
10002, 'hewlett packard',
'gateway') result
from suppliers;
上述解码的语句与以下的if - then- else语句是等效的:
if supplier_id = 10000 then
result := 'ibm';
elsif supplier_id = 10001 then
result := 'microsoft';
elsif supplier_id = 10002 then
result := 'hewlett packard';
else
result := 'gateway';
end if;
decode函数将一个接一个比较每个supplier_id价值。