以下操作在Oracle9i、10g版本成功
步骤一:创建存放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;
/