mel*_*ira 31 sql oracle plsql if-statement while-loop
我尝试了以下代码的不同方式,比如取出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;
/
Run Code Online (Sandbox Code Playgroud)
错误:
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"
Run Code Online (Sandbox Code Playgroud)
通过更改我将变量"a"声明为:
a varchar2(2000);
Run Code Online (Sandbox Code Playgroud)
L P*_*tre 40
PL/SQL:数字或值错误:字符串缓冲区太小
是因为你声明一个字符串是一个固定的长度(比如20),并且在你的代码中的某个时刻你给它赋一个长度超过你声明的值的值.
例如:
myString VARCHAR2(20);
myString :='abcdefghijklmnopqrstuvwxyz'; --length 26
Run Code Online (Sandbox Code Playgroud)
会发生这样的错误
use*_*735 13
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;
/
Run Code Online (Sandbox Code Playgroud)
切勿使用char
.对于理由,请检查以下问题(另请阅读链接):