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

前端 未结 2 1320
闹比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).

    0 讨论(0)
  • 2020-12-12 03:06

    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;
    
    0 讨论(0)
提交回复
热议问题