第一个:01
注意第8行第二个decode中的'2'
SQL> declare
2 x integer;
3 y integer;
4 z varchar2(3);
5 begin
6 x := 1;
7 y := 2;
8 select decode(x,1,decode(y,1,11,'2','12',3,'13',0),0)
9 into z
10 from dual;
11 dbms_output.put_line(x);
12 dbms_output.put_line(y);
13 dbms_output.put_line(z);
14 end;
15 /
1
2
12
PL/SQL 过程已成功完成。
第二个:02
SQL> declare
2 x integer;
3 y integer;
4 z varchar2(3);
5 begin
6 x := 1;
7 y := 2;
8 select decode(x,1,decode(y,1,11,'2','12f',3,'13',0),0)
9 into z
10 from dual;
11 dbms_output.put_line(x);
12 dbms_output.put_line(y);
13 dbms_output.put_line(z);
14 end;
15 /
declare
*
ERROR 位于第 1 行:
ORA-01722: 无效数字
ORA-06512: 在line 8
第三个:03
注意第3.7和8行
SQL> declare
2 x integer;
3 y char(4);
4 z varchar2(3);
5 begin
6 x := 1;
7 y := 2;
8 select decode(x,1,decode(y,1,11,'2','12',3,'13',0),0)
9 into z
10 from dual;
11 dbms_output.put_line(x);
12 dbms_output.put_line(y);
13 dbms_output.put_line(z);
14 end;
15 /
1
2
12
PL/SQL 过程已成功完成。
第四:04
SQL> declare
2 x integer;
3 y char(4);
4 z varchar2(3);
5 begin
6 x := 1;
7 y := '2 ';--此处三个空格
8 select decode(x,1,decode(y,1,11,'2','12',3,'13',0),0)
9 into z
10 from dual;
11 dbms_output.put_line(x);
12 dbms_output.put_line(y);
13 dbms_output.put_line(z);
14 end;
15 /
1
2
12
PL/SQL 过程已成功完成。
第五:05
注意第3.7.8行
SQL> declare
2 x integer;
3 y char(4);
4 z varchar2(3);
5 begin
6 x := 1;
7 y := '2 ';--此处三个空格
8 select decode(x,1,decode(y,1,11,2,'12',3,'13',0),0)
9 into z
10 from dual;
11 dbms_output.put_line(x);
12 dbms_output.put_line(y);
13 dbms_output.put_line(z);
14 end;
15 /
1
2
12
PL/SQL 过程已成功完成
.........
.......
我本以为我对decode有一定了解的
但现在我彻底糊涂了
请大家解释
---------------------------------------------------------------
before assign value to z , the value should be convert to the datatype of X which is integer .
so '12f' can not be convert to numberic value . (note although the value is not stored in x )
---------------------------------------------------------------
我想可能是decode的返回值类型与第三个参数类型相同。所以其余的候选返回值在选中后(!)需要进行类型转换。
decode(1,1,11,2,'bb')不会出错
decode(2,1,11,2,'bb')就会出错
原因就是根据第三个参数(11)确定了decode的返回值是number型,在把'bb'转换成number时出错了。
这大概可以解释你遇到的现象?
----------------------------------
建议不要把问题搞复杂,你的贴子可以用来讨论2个基本问题
1. 变量的赋值
只有select into 和 = 会改变变量的值,所以x和y不会变
,decode 语句当然不会做这件事
2. 隐式类型转换
ORACLE支持隐式类型转换,所以你的字符串可以被数值型变量接受.
'12' --> 12
'12Adfd' --> error (那时自然,ORACLE 也不是神仙吗)