XMLAGG cannot work in PL/SQL in UTL_FILE
403164May 6 2004 — edited May 14 2008I developed a simple xml file generation procedure from my purchase order master and detail tables in my database.
query:
SELECT XMLELEMENT("OrderMessage", XMLATTRIBUTES('updateorder' as "messageType"),
XMLELEMENT("MessageHeader", XMLATTRIBUTES(e.version as "version",s_timestamp as "payloadId" ,
c_timestamp as "timeStamp" ,e.index1 as "senderName"
,e.index2 as "senderComponent",e.documentReferenceId as "documentReferenceId"
,e.singleTransaction as "singleTransaction"),
XMLELEMENT ( "HeaderIndexedAttribute", XMLATTRIBUTES('0' as "index", e.index0 as "content")),
XMLELEMENT ( "HeaderIndexedAttribute", XMLATTRIBUTES('1' as "index", e.senderName as "content")),
XMLELEMENT ( "HeaderIndexedAttribute", XMLATTRIBUTES('2' as "index", e.senderComponent as "content")),
XMLELEMENT ( "HeaderIndexedAttribute", XMLATTRIBUTES('3' as "index", e.index3 as "content")))
,XMLELEMENT("Order", XMLATTRIBUTES(e.closed as "closed",e.orderNumber as "orderNumber",e.ordertype as "orderType",e.uniqueBusinessKey as "uniqueBusinessKey",'SOA+' as "handlingCode")
, (SELECT XMLAGG(XMLELEMENT ("OrderLine", XMLATTRIBUTES (f.lineNumber as "lineNumber"),
XMLELEMENT ( "OrderLineMilestone", XMLATTRIBUTES(f.scheduledDateTime as "scheduledDateTime")),
XMLELEMENT ( "IndexedAttribute", XMLATTRIBUTES('0' as "index", f.extAmt as "content")) ))
FROM PO_DETAIL_TEMP f where f.PAYLOADID = po_payloadId)
, XMLELEMENT ( "Buyer", XMLATTRIBUTES(e.BUYERID as "Id", e.BUYERPARTYNAME as "PartyName", e.BUYERROLE as "Role"))
)).getClobVal() into v_xml
FROM PO_MASTER_TEMP e
where e.PAYLOADID = po_payloadId;
Utl_File.Put_line(v_file, '<!DOCTYPE OrderMessage SYSTEM "OrderMessage.dtd">');
WHILE v_more LOOP
Utl_File.Put(v_file, Substr(v_xml, 1, 32767));
IF Length(v_xml) > 32767 THEN
v_xml := Substr(v_xml, 32768);
ELSE
v_more := FALSE;
END IF;
END LOOP;
it has displayed this error
ERROR at line 1:
ORA-29282: invalid file ID
ORA-06512: at "SYS.UTL_FILE", line 18
ORA-06512: at "SYS.UTL_FILE", line 562
ORA-06512: at "PLANAR.SEND_SO_POSITIVE", line 82
ORA-29285: file write error
ORA-06512: at line 1
the bolded portion is the one giving me problem. i don't understand the logic of this error. if I use SYS_XMLAGG instead of XMLAGG, it writes into the xml file perfectly but it will write a <ROWSET> tag into the file too which isn't needed. I have been running this query in sqlplus directly without problem, only when I use it in pl/sql by storing in a blob and inserting into the file will give me the error.
i have checked the code for the UTL_FILE and it's fine.
I have looked through XML Database Developers Guide - Oracle XML DB manual and this is what i have found for XMLAGG
XMLAgg() function is similar to the SYS_XMLAGG() function except that it returns
a forest of nodes, and does not take the XMLFormat() parameter. This function can
be used to concatenate XMLType instances across multiple rows. It also allows an
optional ORDER BY clause to order the XML values being aggregated.
The point is I don't understand how come query using XML_AGG will give me error when both of queriess result are stored as a blob which shouldn't give me any problem.
Thanks in advance for any help or suggestion. Any alternatives suggestions are welcome too.