我尝试了以下不同的方法,比如去掉while或if,但当我把两者放在一起时(if和while),我总是在最后得到错误。
undefine numero
set serveroutput on
accept numero prompt 'Type # between 100 and 999: '
declare
i number:=1;
a char(25);
b char(1);
c varchar2(10);
d number;
begin
c := №
d := length(c);
b := substr(c, i, 1);
while i <= d loop
if b = '1' then
a:= a||'one ';
end if;
i := i+1;
end loop;
dbms_output.put_line('The number is '||a);
end;
/
ERROR:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 13
06502. 00000 - "PL/SQL: numeric or value error%s"
通过改变我对变量"a"的声明方式,FIXED。
a varchar2(2000);
*注意,这里的重要变化是使用VARCHAR2而不是CHAR(不是更大的长度)。根据@user272735 '的回答,这才是关键。
CHAR
是一个固定长度的数据类型,它使用尽可能多的空间。 所以a:= a||'one ';
需要的空间会超过现有的空间。 你的问题可以简化为下面的例子。
declare
v_foo char(50);
begin
v_foo := 'A';
dbms_output.put_line('length of v_foo(A) = ' || length(v_foo));
-- next line will raise:
-- ORA-06502: PL/SQL: numeric or value error: character string buffer too small
v_foo := v_foo || 'B';
dbms_output.put_line('length of v_foo(AB) = ' || length(v_foo));
end;
/
千万不要使用char
。为了说明理由,请查看以下问题(也请阅读链接)。