步驟一:創建存放XML文件的目錄對象
SQL>createorreplacedirectoryexp_diras'/opt';/
--注意此目錄要有Oracle用戶的寫權限
步驟二:創建過程生成XML文件的主過程
SQL>createorreplaceproceduretable2XML(result inoutnocopyclob,
filenameinvarchar2)is
XMLstr varchar2(32767);
line varchar2(2000);
fhandle utl_file.file_type;
begin
fhandle := utl_file.fopen('EXP_DIR', filename,'w');
XMLstr := dbms_lob.substr(result,32767);
loop
exitwhenXMLstrisnull;
line := substr(xmlstr,1, instr(XMLstr, chr(10)) -1);
dbms_output.put_line('| '|| line);
utl_file.put_line(fhandle, line);
xmlstr := substr(xmlstr, instr(XMLstr, chr(10)) +1);
endloop;
utl_file.fclose(fhandle);
end;
/
步驟三:調用此過程
declare
queryctx dbms_XMLquery.ctxType;
result clob;
begin
queryctx := dbms_xmlquery.newContext('select user_id,user_name from lq_XML_test');
--存放查詢記錄集的SQL
result := dbms_xmlquery.getXML(queryctx);
table2xml(result,'table2xml.XML');
dbms_XMLquery.closeContext(queryctx);
end;
/