SQL> begin
  2   if '   2     ' = 2 then
  3    dbms_output.put_line('hoooooooo');
  4   end if;
  5  end;
  6  /
hooooooooPL/SQL 过程已成功完成。

解决方案 »

  1.   

    我觉得这和decode语句没有关系,是oracle在运行pl/sql时对字符型和整型的约束并不是很严密.即,一个字符可以附值给一个整型的变量运行时却不报错,反之也一样。
      

  2.   

    SQL> begin
      2   if '   2     ' = 2 then
      3    dbms_output.put_line('hoooooooo');
      4   end if;
      5  end;
      6  /
    hoooooooo
    这个也是同样的道理,oracle在编译运行该语句时,把‘    2   ‘作为字符处理,而不是字符串,所以自动去除多余的空格
      

  3.   

    declare
    x integer;
    y integer;
    z varchar2(3);
    begin
    x := 1;
    y := 2;
    select decode(x,1,decode(y,1,11,'2','12f',3,'13',0),0)
    into z
    from dual;
    dbms_output.put_line(x);
    dbms_output.put_line(y);
    dbms_output.put_line(z);
    end;以上语句中,x是integer型,那理想当然不能符字符型给它.
    decode(x,1,decode(y,1,11,'2','12f',3,'13',0),0)
    其实相当于:
    if x=1 then
    x:='12f';
    else
    x:=0;
    end if;而其它也不感奇怪,oracle数据类型是弱类型,可以稳式转换数字型与字符型。
      

  4.   

    to: beckhambobo(beckham)
    以你的说法那么x 应该等于12而不是1啊
      

  5.   

    decode 的执行顺序真得是这样吗decode(x,1,decode(y,1,11,2,'12',3,'13',0),0)
      
    if x =1 then 
      if y = 1 then
         z := 11;
      elsif y = 2 then
         z := 12;
      ...
      end if
    else
       z := 0 ;
    end if;
    如果是那么z := '12f' 应该不报错我主要是这个想不通别的都好理解
      

  6.   

    decode(x,1,decode(y,1,11,2,'12',3,'13',0),0)
    相当于:
    if x=1 then
      if y=1 then
      y:=11;
      elsif y=2 then
      y:='12';
      elsif y=3 then
      y:='13';
      else
      y:=0
      end if;
      x:=y;  --函数不是赋值给y,只是理解方便
    else
    x:=0;
    end if;
      

  7.   

    to: beckhambobo(beckham)
    以你的说法那么x 应该等于12y 也应该等于12而不是1和2啊
      

  8.   


    to: beckhambobo(beckham)
      x:=y;  --函数不是赋值给y,只是理解方便看来也只能这么理解了真不知道oracle是怎么实现decode函数的
      

  9.   

    建议不要把问题搞复杂,你的贴子可以用来讨论2个基本问题1. 变量的赋值
       只有select into 和 = 会改变变量的值,所以x和y不会变
       ,decode 语句当然不会做这件事2. 隐式类型转换
       ORACLE支持隐式类型转换,所以你的字符串可以被数值型变量接受.   '12' --> 12
       '12Adfd' --> error (那时自然,ORACLE 也不是神仙吗)
      

  10.   

    decode函数不会改变列值,只是作显示作用。
    要改变列值还是用update语句.
      

  11.   

    我也知道这些原本也是这么理解的但这句就不能解释了8   select decode(x,1,decode(y,1,11,'2','12f',3,'13',0),0)
      9   into z
     10   from dual;
     
    z 是varchar2z := '12f'不应报错的 
      

  12.   

    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 )
      

  13.   

    我想可能是decode的返回值类型与第三个参数类型相同。所以其余的候选返回值在选中后(!)需要进行类型转换。decode(1,1,11,2,'bb')不会出错
    decode(2,1,11,2,'bb')就会出错原因就是根据第三个参数(11)确定了decode的返回值是number型,在把'bb'转换成number时出错了。这大概可以解释你遇到的现象?
      

  14.   

    这个时oracle内置的数据转换的漏洞  不是decode的错  所以  最好写语句的时候  做显式的数据转换  避免隐患
      

  15.   

    MountLion(闷头睡) 你的解释还是能让有点头绪的