Mohak
Mohak

Reputation: 1

PLSQL procedure to write to File XML Data - some data sometimes gets missed

I have a PLSQL procedure that is reading data from an underlying table, and writing the same to a file, which it then pushes to oracle object storage. This procedure is called multiple times in loop via an Integration in Oracle Integration Cloud. Sample procedure code present below:

declare
    Lv_file UTL_FILE.file_type;
    P_SubstrVal VARCHAR2(10000);
    lv_file_name VARCHAR2 (100):= file_name;
    P_Directory VARCHAR2(101) :='TEMP';
    l_columnValClob CLOB;
    P_offset NUMBER  :=1;
    P_amount NUMBER  :=3000;
    l_clobLen NUMBER :=0;
    c_value NUMBER ;
    l_cursor   SYS_REFCURSOR;
    TYPE fetched_data IS RECORD (
          xml_data   clob,
          created_date date,
          updated_date date
       );
    TYPE fetched_table IS TABLE OF fetched_data
       INDEX BY PLS_INTEGER;
    l_table_data fetched_table;
BEGIN
    BEGIN
        DBMS_CLOUD.DELETE_FILE('TEMP',file_name);
    EXCEPTION
        WHEN OTHERS THEN
            null;
    END;
    Lv_file := utl_file.fopen(P_Directory,lv_file_name,'wb');
    utl_file.put_raw(Lv_file,utl_raw.cast_to_raw('<?xml version="1.0" encoding="UTF-8"?><wd:DATA>'));
    OPEN l_cursor FOR 'SELECT XML_DATA,CREATED_DATE,UPDATED_DATE FROM (SELECT a.*, ROWNUM rnum  FROM (SELECT XML_DATA,CREATED_DATE,UPDATED_DATE FROM ' || table_name || ' order by employee_id) a WHERE ROWNUM <= ' || ending_row || ') WHERE rnum >= ' || starting_row ;
    FETCH l_cursor BULK COLLECT INTO l_table_data;
    CLOSE l_cursor;
    RECORD_COUNT := l_table_data.COUNT;
    FOR indx IN 1 .. l_table_data.COUNT
    LOOP
        P_offset := 1;
        l_columnValClob := l_table_data (indx).XML_DATA;
        l_clobLen := DBMS_LOB.GETLENGTH(l_columnValClob);
        WHILE P_offset < l_clobLen
        LOOP
           P_SubstrVal := dbms_lob.substr(l_columnValClob,P_amount,P_offset);
           utl_file.put_raw(Lv_file,utl_raw.cast_to_raw(P_SubstrVal));
           P_offset:=P_offset+P_amount;
        END LOOP;
    End loop;
    utl_file.put_raw(Lv_file,utl_raw.cast_to_raw('</wd:DATA>'));
    UTL_FILE.fclose(Lv_file);
    -- some more code
end;

The issue I am facing is that sometimes the file generated has some missing or extra XML data, - the syntax validation for the XML fails. Example: Error example image - Error in XML - data is missing because of which the tags are incorrect

The main issue is, that sometimes the procedure works perfectly fine, and sometimes, some files have this kind of incorrect data.

Please tell me what is the issue here, and how we can fix it...

Upvotes: 0

Views: 270

Answers (0)

Related Questions