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

前端 未结 2 1328
闹比i
闹比i 2020-12-12 02:09

I implemented a function that returns clob data-type, and I would like to print the result in DBMS Output. Unfortunately, I am getting

2条回答
  •  鱼传尺愫
    2020-12-12 02:52

    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).

提交回复
热议问题