decode用法: 1。decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)
该函数的含义如下:
IF 条件=值1 THEN
RETURN(返回值1)
ELSIF 条件=值2 THEN
RETURN(返回值2)
......
ELSIF 条件=值n THEN
RETURN(返回值n)
ELSE
RETURN(缺省值)
END IF
eg:select decode('&n','1','hello','2','world') from dual;当你n传入参数为1的时候会输出hello,n=2时输出world。 2.decode(表达式a,b,c,d)
这个的意思是当表达式a的值等于‘b的时候显示‘c’否则显示‘d’
eg:select decode(sum(&a+&b),10,'the sum is 10','the sum is not 10') from dual;当你的输入a+b=10的时候输出'the sum is 10'。否则输出the sum is not 10
decode和case when .... then else ....end比较
eg:select e.* ,case when e.deptno = 20 then
'e.sal >= 1500 '
else
'e.sal>2500'
end as sals from emp e;
decode用的比较多的地方就是取值判断。例如:user表的status字段为0时显示禁用,为1是显示可用。直接用decode函数取值,到前台显示的时候就不用再if esle的判断了。