Reputation: 535
I'm trying to extract data from Oracle SQL tables which is working to a point when I put the write to file outside the loop like the example below. But in the code below this code when I put the write to file inside loop it falls over with ORA-31181 error.
What I'm trying to do on a bigger scale is get a load of XML to a file by writing to a clob outputting it to a flat file every time it goes around the loop.
DECLARE -- this version of code works as the write to is outside loop
l_file UTL_FILE.FILE_TYPE;
l_clob CLOB;
l_buffer VARCHAR2(32767);
l_amount BINARY_INTEGER := 32767;
l_pos INTEGER := 1;
l_extract_dir CONSTANT dba_directories.directory_name%TYPE:= 'REPORTS_OUT_DIR'; -- \\data2\data\download\d7prdv1\prsrepreports
l_xmltype XMLTYPE;
l_domdoc dbms_xmldom.DOMDocument;
l_root_node dbms_xmldom.DOMNode;
l_message_node dbms_xmldom.DOMNode;
l_production_element dbms_xmldom.DOMElement;
l_production_node dbms_xmldom.DOMNode;
-- production XML elements, node, text
l_prod_element dbms_xmldom.DOMElement;
l_prod_node dbms_xmldom.DOMNode;
l_prod_t_node dbms_xmldom.DOMNode;
l_prod_text dbms_xmldom.DOMText;
-- production XML elements, node, text, node
CURSOR c_production
IS SELECT prod.cre_surr_id as cre_surr_id
, prod.production_type as prodCategoryType
from productions prod
where prod.cre_surr_id in (1753959927,1753959929);
BEGIN
UTL_FILE.FCLOSE_ALL;
l_file := UTL_FILE.fopen(l_extract_dir , 'Sample2.dat', 'w', 32767);
-- Create an empty XML document
l_domdoc := dbms_xmldom.newDomDocument;
-- Create a root node
l_root_node := dbms_xmldom.makeNode(l_domdoc);
-- Create a message root node
l_message_node := dbms_xmldom.appendChild( l_root_node
, dbms_xmldom.makeNode(dbms_xmldom.createElement(l_domdoc, 'message' ))
);
FOR production_rec in c_production LOOP
l_production_element := dbms_xmldom.createElement(l_domdoc, 'production' );
l_production_node := dbms_xmldom.appendChild(l_message_node,dbms_xmldom.makeNode(l_production_element));
-- prodCategoryType
l_prod_element := dbms_xmldom.createElement(l_domdoc, 'prodCategoryType' );
l_prod_node := dbms_xmldom.appendChild(l_production_node,dbms_xmldom.makeNode(l_prod_element));
l_prod_text := dbms_xmldom.createTextNode(l_domdoc, production_rec.prodCategoryType );
l_prod_t_node := dbms_xmldom.appendChild(l_prod_node,dbms_xmldom.makeNode(l_prod_text));
END LOOP;
l_xmltype := dbms_xmldom.getXmlType(l_domdoc);
dbms_xmldom.freeDocument(l_domdoc);
l_clob := l_xmltype.getClobVal;
DBMS_LOB.read (l_clob, l_amount, l_pos, l_buffer);
UTL_FILE.put(l_file, l_buffer);
l_pos := l_pos + l_amount;
UTL_FILE.fclose(l_file);
END;
The code below creates an error message when I put the write to file inside the loop.
The error message is below.
Error at line 1 ORA-31181: PL/SQL DOM handle accesses node that is no longer available ORA-06512: at "XDB.DBMS_XMLDOM", line 4735 ORA-06512: at "XDB.DBMS_XMLDOM", line 4762 ORA-06512: at line 52
DECLARE -- this version of code DOES not work and creates the ORA-31181 error
l_file UTL_FILE.FILE_TYPE;
l_clob CLOB;
l_buffer VARCHAR2(32767);
l_amount BINARY_INTEGER := 32767;
l_pos INTEGER := 1;
l_extract_dir CONSTANT dba_directories.directory_name%TYPE:= 'REPORTS_OUT_DIR'; -- \\data2\data\download\d7prdv1\prsrepreports
l_xmltype XMLTYPE;
l_domdoc dbms_xmldom.DOMDocument;
l_root_node dbms_xmldom.DOMNode;
l_message_node dbms_xmldom.DOMNode;
l_production_element dbms_xmldom.DOMElement;
l_production_node dbms_xmldom.DOMNode;
-- production XML elements, node, text
l_prod_element dbms_xmldom.DOMElement;
l_prod_node dbms_xmldom.DOMNode;
l_prod_t_node dbms_xmldom.DOMNode;
l_prod_text dbms_xmldom.DOMText;
-- production XML elements, node, text, node
CURSOR c_production
IS SELECT prod.cre_surr_id as cre_surr_id
, prod.production_type as prodCategoryType
from productions prod
where prod.cre_surr_id in (1753959927,1753959929);
BEGIN
UTL_FILE.FCLOSE_ALL;
l_file := UTL_FILE.fopen(l_extract_dir , 'Sample2.dat', 'w', 32767);
-- Create an empty XML document
l_domdoc := dbms_xmldom.newDomDocument;
-- Create a root node
l_root_node := dbms_xmldom.makeNode(l_domdoc);
-- Create a message root node
l_message_node := dbms_xmldom.appendChild( l_root_node
, dbms_xmldom.makeNode(dbms_xmldom.createElement(l_domdoc, 'message' ))
);
FOR production_rec in c_production LOOP
l_production_element := dbms_xmldom.createElement(l_domdoc, 'production' );
l_production_node := dbms_xmldom.appendChild(l_message_node,dbms_xmldom.makeNode(l_production_element));
-- prodCategoryType
l_prod_element := dbms_xmldom.createElement(l_domdoc, 'prodCategoryType' );
l_prod_node := dbms_xmldom.appendChild(l_production_node,dbms_xmldom.makeNode(l_prod_element));
l_prod_text := dbms_xmldom.createTextNode(l_domdoc, production_rec.prodCategoryType );
l_prod_t_node := dbms_xmldom.appendChild(l_prod_node,dbms_xmldom.makeNode(l_prod_text));
l_xmltype := dbms_xmldom.getXmlType(l_domdoc);
dbms_xmldom.freeDocument(l_domdoc);
l_clob := l_xmltype.getClobVal;
DBMS_LOB.read (l_clob, l_amount, l_pos, l_buffer);
UTL_FILE.put(l_file, l_buffer);
l_pos := l_pos + l_amount;
END LOOP;
UTL_FILE.fclose(l_file);
END;
Upvotes: 1
Views: 1332
Reputation: 6356
Easiest way.
begin
for rec in ( SELECT xmlserialize(content xmlelement("production",
xmlelement("prodCategoryType",prod.production_type)) as varchar2(4000)) val from productions prod ) loop
dbms_output.put_line(rec.val) ;
end loop;
end;
Or in your code add v_temp DBMS_XMLDOM.DOMNODE;
in declaration and replace dbms_xmldom.freeDocument(l_domdoc);
with temp:= DBMS_XMLDOM.REMOVECHILD(l_message_node,l_production_node);
Upvotes: 0