Oracle - ORA-06502: PL/SQL: numeric or value error (DBMS_OUTPUT)

做~自己de王妃 提交于 2019-11-28 14:32:18

You will not be able to print a clob using dbms_output.put_line directly if it is greater than 32767 bytes.

If this is the case you can create a procedure to iterate through the clob and print out one smaller chunk at a time. Such a procedure and test script is below:

declare 

  c clob;

  procedure print_clob( p_clob in clob ) is
      v_offset number default 1;
      v_chunk_size number := 10000;
  begin
      loop
          exit when v_offset > dbms_lob.getlength(p_clob);
          dbms_output.put_line( dbms_lob.substr( p_clob, v_chunk_size, v_offset ) );
          v_offset := v_offset +  v_chunk_size;
      end loop;
  end print_clob;


begin
  for i in 1..10000 loop
     c := c || 'test';
  end loop;
  --This will result in ora-06502
  --dbms_output.put_line(c);

  print_clob(c);

end;

Note that v_chunk_size must result in less than 32767 bytes being chunked at-a-time. If you encoding has 2 bytes per char you will need to use (32767/2).

The following procedure will better:

  • Oracle 10g has limitation on put_line (a maximum of 32767 characters), But Oracle before 10g has a maximum of 255 characters limitation.
  • the 'put_line' adding end of line on every iteration loop during output clob. So we use put() better (and 'DBMS_OUTPUT.NEW_LINE' at end).
    PROCEDURE print_clob( p_clob in clob ) IS
        v_offset number default 1;
        v_chunk_size number := 255; 
      BEGIN
        LOOP
          EXIT when v_offset > dbms_lob.getlength(p_clob);
          dbms_output.put( dbms_lob.substr( p_clob, v_chunk_size, v_offset ) );
          v_offset := v_offset +  v_chunk_size;
        END LOOP;
        DBMS_OUTPUT.NEW_LINE;  
      END print_clob;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!