我已經繼承了在後端使用Oracle數據庫的應用程序。該數據庫最初使用的是Oracle的完整版本,但我們已將其移交給Oracle XE。 Oracle XE似乎不支持Oracle XML SQL Utility(其中包括DBMS_XMLSave),或者至少我一直無法弄清楚如何安裝它。將Oracle存儲過程從使用DBMS_XMLSave轉換爲DBMS_XMLStore
根據這個鏈接(http://ellebaek.wordpress.com/2011/01/27/converting-between-oracle-data-and-xml/):
像DBMS_XMLQUERY,DBMS_XMLSAVE是用Java實現,因此它不能與Oracle數據庫快捷版的支持。
在數據庫中的存儲過程中使用DBMS_XMLSave,但是從這個鏈接(https://forums.oracle.com/forums/thread.jspa?threadID=530048)看來,DBMS_XMLSave已取代DBMS_XMLStore:
的DBMS_XMLSTORE PL/SQL包在Oracle數據庫10g中引入版本1.該軟件包根據XML文檔的內容對數據庫內的關係表或對象表執行DML操作。
請注意,在Oracle數據庫10g之前,此功能存在於另一個名爲DBMS_XMLSAVE的PL/SQL包中。
我試圖在鏈接的解決方案:
CREATE OR REPLACE PUBLIC SYNONYM DBMS_XMLSAVE FOR DBMS_XMLSTORE;
GRANT EXECUTE ON DBMS_XMLSAVE TO PUBLIC;
這並修復錯誤的很大一部分,但我留下了幾個問題行:
DBMS_XMLSave.setDateFormat(v_insCtx, 'MM/dd/yyyy');
DBMS_XMLSave.setBatchSize(v_updCtx, -1);
DBMS_XMLSave.setDateFormat(v_updCtx, 'MM/dd/yyyy');
DBMS_XMLStore做似乎不支持這些方法。挖掘周圍我想我已經找到了解決setDateFormat問題的方法。根據此鏈接(http://ellebaek.wordpress.com/2011/01/27/converting-between-oracle-data-and-xml/),DBMS_XMLStore使用日期/時間值的NLS設置。此鏈接(http://www.tiplib.com/231/nls-parameter-session-logon-trigger)指出,NLS設置可以在存儲過程中被改變這樣的:
BEGIN
DBMS_SESSION.SET_NLS('NLS_DATE_FORMAT','YYYYMMDD');
COMMIT;
END;
所以我希望我可以代替setDateFormat線是這樣的:
-DBMS_XMLSave.setDateFormat(v_insCtx, 'MM/dd/yyyy'); -- remove
+DBMS_SESSION.SET_NLS('NLS_DATE_FORMAT','MM/dd/yyyy'); -- insert
我無法找到setBatchSize的替代品。
所以我的問題是:
上午我在正確的方式處理這個,或者是有沒有更好的方法?
請問DBMS_XMLSave.setDateFormat(v_insCtx, 'MM/DD/YYYY') - > DBMS_SESSION.SET_NLS( 'NLS_DATE_FORMAT', 'MM/DD/YYYY')轉變工作?
是否有替代setBatchSize?是否有必要更換setBatchSize?
一般來說,如果有人問這樣的問題2問題上面我會告訴他們去嘗試,並告訴我,但我目前還不能得到代碼編譯,我沒有一個已知的工作基礎。我可以刪除問題行,它可能會產生一些結果,但我不清楚結果是否正確。刪除問題行並嘗試檢查結果將是我從這裏開始的方法。
原來的代碼如下:
procedure insert_xml(p_xmlDoc in clob, p_tableName in varchar2) is
v_insCtx DBMS_XMLSave.ctxType;
v_rows number;
begin
v_insCtx := DBMS_XMLSave.newContext(p_tableName); -- get the context handle
DBMS_XMLSave.setDateFormat(v_insCtx, 'MM/dd/yyyy'); -- set date format
v_rows := DBMS_XMLSave.insertXML(v_insCtx, p_xmlDoc); -- this inserts the document
DBMS_XMLSave.closeContext(v_insCtx); -- this closes the handle
exception
when OTHERS then
raise_application_error(-20001,'An error was encountered. - '||SQLCODE||' -ERROR- '||SQLERRM);
end insert_xml;
procedure update_xml(p_xmlDoc in clob, p_tableName in varchar2, p_key in varchar2) is
v_updCtx DBMS_XMLSave.ctxType;
v_rows number;
begin
v_updCtx := DBMS_XMLSave.newContext(p_tableName); -- get the context
DBMS_XMLSave.setBatchSize(v_updCtx, -1);
DBMS_XMLSave.setDateFormat(v_updCtx, 'MM/dd/yyyy'); -- set date format
DBMS_XMLSave.clearUpdateColumnList(v_updCtx); -- clear the update settings..
DBMS_XMLSave.setKeyColumn(v_updCtx, p_key); -- set EMPNO as key column
v_rows := DBMS_XMLSave.updateXML(v_updCtx, p_xmlDoc); -- update the table.
DBMS_XMLSave.closeContext(v_updCtx); -- close the context..!
exception
when OTHERS then
raise_application_error(-20001,'An error was encountered. - '||SQLCODE||' -ERROR- '||SQLERRM);
end update_xml;
感謝您的幫助。