I implemented a function that returns clob data-type, and I would like to print the result in DBMS Output. Unfortunately, I am getting
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:
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;