淺談如何將Oracle匯出到XML檔案

來源:互聯網
上載者:User

以下操作在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;
/

  1. 詳解Oracle的幾種分頁查詢語句
  2. 實戰OracleDatabase Backup與恢複
  3. Oracle DBA職責及日常工作分析

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.