Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not able to write a big size data with UTL_FILE.PUT_LINE

I created a xml which includes a big amount of data. Now I am trying to write that generated xml into a file.

Declaration:

prodFeed_file  UTL_FILE.FILE_TYPE;
prodFeed_file := UTL_FILE.FOPEN ('CSV_DIR', 'feed.xml', 'w', 32767); 

WRITING INTO FILE:

UTL_FILE.PUT_LINE(prodFeed_file,l_xmltype.getClobVal);

UTL_FILE.FCLOSE(prodFeed_file);

If l_xmltype.getClobVal returns limited record then it is working file but if the l_xmltype.getClobVal exceeds the size(almost 35 KB) it is giving an error:

ORA-06502: PL/SQL: numeric or value error

like image 933
Ram Dutt Shukla Avatar asked Dec 20 '22 16:12

Ram Dutt Shukla


1 Answers

The UTL_FILE documentation says:

"The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. "

You will have to write the CLOB chunk by chunk. Something like this:

DECLARE
  v_clob CLOB;
  v_clob_length INTEGER;
  pos INTEGER := 1;
  buffer VARCHAR2(32767);
  amount BINARY_INTEGER := 32760;
  prodFeed_file utl_file.file_type;
BEGIN
  prodFeed_file := UTL_FILE.FOPEN ('CSV_DIR', 'productFeedLargo.xml', 'w', 32767);
  v_clob := l_xmltype.getClobVal;
  v_clob_length := length(v_clob);

  WHILE pos < v_clob_length LOOP
    dbms_lob.read(v_clob, amount, pos, buffer);
    utl_file.put(prodFeed_file , char_buffer);
    utl_file.fflush(prodFeed_file);
    pos := pos + amount;
  END LOOP;

  utl_file.fclose(prodFeed_file);

END;
/
like image 53
pablomatico Avatar answered Jan 05 '23 00:01

pablomatico