1、case
语法:
case
when 条件1 then 返回值1
when 条件2 then 返回值2
...
else 返回值N
end;
示例:
declare
i integer;
str varchar2(20);
begin
i := 3;
str := case
when i = 1 then ‘a‘
when i = 2 then ‘b‘
when i = 3 then ‘c‘
end ;
dbms_output.put_line(str);
end;
CASE的后台实现代码:
if (condition1) {
‘Is Positive‘;
} else if (condition2 ) {
‘Is Negative‘;
}else {
‘Is Zero’
}
2、Decode
语法:decode(变量或表达式,值1,返回值1,值2,返回值2,...,默认值)
示例:SELECT DECODE(SIGN(5 – 5), 1, ‘Is Positive‘, -1, ‘Is Negative‘, ‘Is Zero’) FROM DUAL
Decode的后台实现:
switch ( SIGN(5 – 5) )
{
case 1 : ‘Is Positive‘; break;
case 2 : ‘Is Negative‘; break;
default : ‘Is Zero’
}