1.oracle中的decode是什么?2.如果回答是函数,那么我可不可以这样写:declare
v_test number :=3;
v_mes varchar2(10);
beginv_mes :=decode(v_test,3,'aaa','fafa');
dbms_output.put_line(v_mes);
end;请给出比较详细的解答,谢谢各位!
v_test number :=3;
v_mes varchar2(10);
beginv_mes :=decode(v_test,3,'aaa','fafa');
dbms_output.put_line(v_mes);
end;请给出比较详细的解答,谢谢各位!
2.你的用法也是对的可以参考http://xindongyinfu.javaeye.com/blog/564450
decode(v_test,3,'aaa','fafa')可以理解为
IF V_TEST=3 THEN
v_mes='aaa'
ELSE
v_mes='fafa'
end if
Decode(column_name,comparison,action,comparison,action,. . .else action)
--把column_name的内容与每一个comparison进行比较.如果相等, decode就执行action动作.如果没有任何一项与comparsion匹配,程序就执行else action动作
我的那种写法是不正确的。
oracle 11g下已经验证。
v_test number :=3;
v_mes varchar2(10);
begin
select decode(v_test,3,'aaa','fafa') into v_mes from dual;
dbms_output.put_line(v_mes);
end;
这样用,DECODE肯定是函数
In Oracle/PLSQL, the decode function has the functionality of an IF-THEN-ELSE statement.The syntax for the decode function is:decode( expression , search , result [, search , result]... [, default] )expression is the value to compare.search is the value that is compared against expression.result is the value returned, if expression is equal to search.default is optional. If no matches are found, the decode will return default. If default is omitted, then the decode statement will return null (if no matches are found).Applies To:Oracle 9i, Oracle 10g, Oracle 11g
SQL和PL/SQL中自带很多类型的函数,有字符、数字、日期、转换、和混合型等多种函数用于处理单行数据,因此这些都可被统称为单行函数。这些函数均可用于SELECT,WHERE、ORDER BY等子句中;
decode 函数属于单行函数,需要配合SELECT,WHERE、ORDER BY等子句使用。
v_mes varchar2(10);
begin
v_mes :=to_char(12312);
dbms_output.put_line(v_mes);
end;
to_char()也是函数单行函数吧。我可以像我上边的那样写。为什么decode这个函数不能赋值给变量呢?