oracle 合併多個sys refcursor

2021-09-13 19:15:05 字數 2639 閱讀 8285

在資料開發中,有時你需要合併兩個動態游標sys_refcursor

開發乙個儲存過程proc_a,這個過程業務邏輯相當複雜,**篇幅較長。一段時間後要開發乙個proc_b,要用proc_a同樣的邏輯,而且在這個過程中,還要迴圈呼叫proc_a這個過程。擺在你面前的有兩個選擇。

好吧,這個新的過程是完成了,可是看上去,它更複雜了,**量更大了。完全不能接受,必須改改!

這時,已經默默開啟了oracle官方幫助文件 尋找乙個可行的辦法,最終目標標是要解析,整合,合併 游標 sys_refcursor

經過搜尋查詢,找到以下可行的方案

序列化sys_refcursor為xml文件,oracle對xml支援還不錯,12c已經有json格式了

使用oracle xml解析的方法,對序列化的xml文件,新增、刪除、修改

轉換為記憶體表,通過游標返回查詢的結果

為此你需要掌握的知識有

從上邊的幫助文件中,知道xmltype的建構函式中可以直接傳入游標xmltype(refcursor)從而得到乙個xmltype,呼叫xmltype的getclobval方法,可得到序列化的結果,所以它的結構是這樣的

<?xml version="1.0"?>

<...>...

....

所以,如果需要合併兩個資料列相同游標,只需要提取dom中的row節點資料儲存到定義的clob欄位中去。

提取dom中片段,採用標準的xpath語法,/*/row這裡提取row資訊

declare

x xmltype;

rowxml clob;

mergexml clob;

ref_cur sys_refcursor;

ref_cur2 sys_refcursor;

ref_cur3 sys_refcursor;

begin

open ref_cur for

select f_username, f_usercode, f_userid

from tb_system_user

where f_userid = 1;

dbms_lob.createtemporary(mergexml, true);

x := xmltype(ref_cur);

dbms_output.put_line('*****完整的refcursor結構*****');

dbms_output.put_line(x.getclobval());

dbms_output.put_line('*****只提取行資訊*****');

rowxml := x.extract('/rowset/row').getclobval(0, 0);

dbms_output.put_line(rowxml);

open ref_cur2 for

select f_username, f_usercode, f_userid

from tb_system_user

where f_userid = 1000;

x := xmltype(ref_cur2);

rowxml := x.extract('/rowset/row').getclobval(0, 0);

dbms_output.put_line('*****合併後的資訊*****');

dbms_output.put_line(mergexml);

end;

執行這段**輸出的結果是這樣的

*****完整的refcursor結構*****

<?xml version="1.0"?>

系統管理員

admin

1 *****只提取行資訊*****

系統管理員

admin

1*****合併後的資訊*****

系統管理員

admin1黃燕

huangyan

1000

從上邊列印的結果看,我們已經成功的將兩個游標ref_curref_cur2中我們需要的列資訊合併到了乙個xml文件中。那麼接下了,我們就需要通過解析這個xml並返回乙個新的sys_refcursor,這裡你有必要了解以下oraclexmltable的用法(

dbms_output.put_line(mergexml);

open ref_cur3 for

select *

from xmltable('/rowset/row' passing xmltype(mergexml) columns

f_username varchar2(100) path 'f_username',

f_usercode varchar2(100) path 'f_usercode');

簡單說明下xmltable建構函式

xml作為早期資料傳輸,序列化和反序列化的檔案格式,在oracle中也有良好的支援。所以,對於基於語言之上的知識,各個語言實現方式基本相識。基礎終究是重要的。

合併多個List

public class listutil catch exception e for int i 0,len lists.length i len i return list 測試方法 public class listutiltest system.out.println list 2018 1...

hdfs 多個檔案合併 合併多個檔案遞迴HDFS

在hdfs我的資料夾路徑結構是這樣的 合併多個檔案遞迴hdfs data topicname year 2017 month 02 day 28 hour 00 data topicname year 2017 month 02 day 28 hour 01 data topicname year ...

合併多個word檔案

unit wordoperate 2008 10 inte ce uses sysutils,comobj,shellapi,windows 合併多個word檔案,存到乙個指定檔案裡 引數說明 arrword 為帶路徑的word檔名稱陣列 outfilename 為合併後的檔名稱 binsertpa...