gpt4 book ai didi

sql - ORA-06502 : character string buffer too small. 即使字符串大小低于声明的大小限制

转载 作者:行者123 更新时间:2023-12-05 00:57:20 26 4
gpt4 key购买 nike

 FOR this_loop
IN (SELECT field_A, field_B
FROM TABLE_NAME
WHERE num = i_num)
LOOP
IF this_loop.field_B BETWEEN 1 AND 3
THEN
v_A := v_A || ' ' || this_loop.field_A;
ELSIF this_loop.field_B BETWEEN 4 AND 8
THEN
v_field_A := v_field_A || ' ' || this_loop.field_A; -- Error is at this line
ELSIF this_loop.field_B BETWEEN 9 AND 15
THEN
v_B := v_B || ' ' || this_loop.field_A;
END IF;
END LOOP;

变量声明为
v_field_A            VARCHAR2 (100);

我知道的 -
  • 变量 v_field_A 的值不能超过 100 个字符
  • 我从 SELECT 得到的输出查询不超过 10 个字符。

  • 我的问题 - 当字符在 varchar2 的限制范围内时,如何解决空间缓冲区的问题?
    几年前我遇到过这个问题,但上次原因是输出 select询问。它有超过 100 个字符,因此存在大小问题,但这次不超过 10 个字符。我糊涂了。任何帮助表示赞赏

    最佳答案

    Variable v_field_A cannot hold value more than 100 characters



    为什么不?很有可能,因为你是 串联 中每一行的变量循环光标 .

    例如,
    SQL> DECLARE
    2 v_name VARCHAR2(50);
    3 BEGIN
    4 FOR i IN
    5 (SELECT ename FROM emp
    6 )
    7 LOOP
    8 v_name := v_name || i.ename;
    9 END LOOP;
    10 END;
    11 /
    DECLARE
    *
    ERROR at line 1:
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    ORA-06512: at line 8

    使用 DBMS_OUTPUT 查看变量的当前大小和附加的新值。

    让我们调试
    SQL> DECLARE
    2 v_name VARCHAR2(50);
    3 BEGIN
    4 FOR i IN
    5 (SELECT ename FROM emp
    6 )
    7 LOOP
    8 dbms_output.put_line('Length of new value = '||LENGTH(i.ename));
    9 v_name := v_name || i.ename;
    10 dbms_output.put_line('Length of variable = '||LENGTH(v_name));
    11 END LOOP;
    12 END;
    13 /
    Length of new value = 5
    Length of variable = 5
    Length of new value = 5
    Length of variable = 10
    Length of new value = 4
    Length of variable = 14
    Length of new value = 5
    Length of variable = 19
    Length of new value = 6
    Length of variable = 25
    Length of new value = 5
    Length of variable = 30
    Length of new value = 5
    Length of variable = 35
    Length of new value = 5
    Length of variable = 40
    Length of new value = 4
    Length of variable = 44
    Length of new value = 6
    Length of variable = 50
    Length of new value = 5

    错误
    DECLARE
    *
    ERROR at line 1:
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    ORA-06512: at line 9

    很明显,我们想要连接长度为 5 的字符串。到声明为最大大小的变量 50 ,当前持有大小为 50 的值.因此,它抛出错误 ORA-06502: PL/SQL: numeric or value error: character string buffer too small .

    关于sql - ORA-06502 : character string buffer too small. 即使字符串大小低于声明的大小限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34720255/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com