oracle 11g ORA-06502:PL/SQL:数字或值错误:字符串缓冲区太小

Posted

技术标签:

【中文标题】oracle 11g ORA-06502:PL/SQL:数字或值错误:字符串缓冲区太小【英文标题】:oracle 11g ORA-06502: PL/SQL: numeric or value error: character string buffer too small 【发布时间】:2021-03-22 08:15:21 【问题描述】:

在oracle 11g r2中,我们想获取所有的列和拼接,它发出:ORA-06502: PL/SQL: numeric or value error: string buffer too small,如何修改?

第一步:创建表:

create table test.history_session as select * from dba_hist_active_sess_history where 1=1;

第 2 步:测试:

declare
column_list1   varchar2(32767);
column_list2   varchar2(32767);
i number;
l number;
BEGIN
column_list1:='';
column_list2:='';
i:=0;
l:=0;
FOR v_column_name IN (SELECT owner, table_name, column_name,data_length,data_type FROM dba_tab_columns WHERE owner ='TEST' and table_name='HISTORY_SESSION'
and data_type in ('VARCHAR2','NVARCHAR2','NUMBER','CHAR') and data_length <=255 order by column_name) LOOP
if v_column_name.data_type='NUMBER' then
v_column_name.column_name:='to_char('||v_column_name.column_name||',''99999999999999999.99'')';
end if;
if l <3700 then
column_list1:=column_list1||v_column_name.column_name||chr(10)||'||''|'''||'||';
else
column_list2:=column_list2||v_column_name.column_name||chr(10)||'||''|'''||'||';
end if;
l:=l+v_column_name.data_length+10;
end loop;
dbms_output.put_Line(nvl(column_list1,' ')||nvl(column_list2,' '));
end;
/ 

错误:

declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 14

我想知道如何修改?

【问题讨论】:

【参考方案1】:

您不能将光标的变量设置为其他东西;这是错误的:

v_column_name.column_name:='to_char('||v_column_name.column_name| ...

改为声明一个局部变量,然后根据需要进行这些转换。像这样(以SCOTT 用户身份运行,所以我改用USER_TAB_COLUMNS)。

SQL> CREATE TABLE history_session
  2  (
  3     id   NUMBER
  4  );

Table created.

SQL> DECLARE
  2     column_list1   VARCHAR2 (32767);
  3     column_list2   VARCHAR2 (32767);
  4     l_column_name  VARCHAR2 (200);          --> local variable
  5     i              NUMBER;
  6     l              NUMBER;
  7  BEGIN
  8     column_list1 := '';
  9     column_list2 := '';
 10     i := 0;
 11     l := 0;
 12
 13     FOR v_column_name IN (  SELECT                                     --owner,
 14                                    table_name,
 15                                    column_name,
 16                                    data_length,
 17                                    data_type
 18                               FROM user_tab_columns
 19                              WHERE     1 = 1
 20                                    --                                  AND owner = 'TEST'
 21                                    AND table_name = 'HISTORY_SESSION'
 22                                    AND data_type IN ('VARCHAR2',
 23                                                      'NVARCHAR2',
 24                                                      'NUMBER',
 25                                                      'CHAR')
 26                                    AND data_length <= 255
 27                           ORDER BY column_name)
 28     LOOP
 29        IF v_column_name.data_type = 'NUMBER'
 30        THEN
 31           --v_column_name.column_name :=
 32           l_column_name :=
 33                 'to_char('
 34              || v_column_name.column_name
 35              || ',''99999999999999999.99'')';
 36        ELSE
 37           l_column_name := v_column_name.column_name;
 38        END IF;
 39
 40        IF l < 3700
 41        THEN
 42           column_list1 :=
 43              column_list1 || l_column_name          --v_column_name.column_name
 44                                            || CHR (10) || '||''|''' || '||';
 45        ELSE
 46           column_list2 :=
 47              column_list2 || l_column_name          --v_column_name.column_name
 48                                            || CHR (10) || '||''|''' || '||';
 49        END IF;
 50
 51        l := l + v_column_name.data_length + 10;
 52     END LOOP;
 53
 54     DBMS_OUTPUT.put_Line (NVL (column_list1, ' ') || NVL (column_list2, ' '));
 55  END;
 56  /
to_char(ID,'99999999999999999.99')
||'|'||

PL/SQL procedure successfully completed.

SQL>

结果看起来不太好,但我会把它留给你 - 修复它,现在该过程有点工作。

【讨论】:

以上是关于oracle 11g ORA-06502:PL/SQL:数字或值错误:字符串缓冲区太小的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 生成 AWR 报告报错:ORA-06502,怎么破?

Oracle 生成 AWR 报告报错:ORA-06502,怎么破?

oracle ORA-06502:PL/SQL:数字或值错误:批量绑定:截断绑定

Oracle 存储过程无法生成 csv 文件 - ORA-06502: PL/SQL: numeric or value error: string buff

Oracle ORA-06502 PL/SQL:数字或值错误:字符到数字的转换错误

[Oracle]发生 ora-06502 RMAN 在对 catalog DB 同期时出错的调查方法