2015-10-27 28 views
0

我有一個存儲過程,它返回一個參考遊標。我需要做的是進一步過濾結果。由於受到保護,我不確定我可以提供多少信息。結果中有一個'Editedflag'列。如果該列值爲'Y',則需要獲取'PassID'列值並找到具有相同'PassID'的其他結果並將其從結果中移除。我對這種類型的問題甚至失去了甚至谷歌。謝謝。根據結果修改oracle的cusor結果

SELECT a.log_curv_id LogCurveId 
     ,a.log_curv_type_id LogCurveTypeId 
     ,c.log_curv_type_desc LogCurveTypeDesc 
     ,a.cmpl_fac_id CompletionId 
     ,d.fac_nme CompletionName 
     ,b.edtd_curv_indc EditedIndicator 
     ,b.stsfr_qlty_indc SatisfactoryIndicator 
     ,a.top_md_qty TopMeasuredDepth 
     ,a.base_md_qty BaseMeasuredDepth 
     ,NVL(TO_NUMBER(eds.p_surv_load.fget_log_curv_spec(a.log_curv_id,2248)),NULL) FlowRate 
     ,NVL(TO_NUMBER(eds.p_surv_load.fget_log_curv_spec(a.log_curv_id,800)),NULL) TubingPressure 
     ,NVL(TO_NUMBER(eds.p_surv_load.fget_log_curv_spec(a.log_curv_id,2249)),NULL) LossAbove 
     ,NVL(TO_NUMBER(eds.p_surv_load.fget_log_curv_spec(a.log_curv_id,2250)),NULL)LossBelow 
     ,b.lggg_tlstr_pass_id PassId 
     FROM eds.log_curv a, 
     eds.acqn_curv b, 
     eds.log_curv_type c, 
     eds.fac_nme d 
     WHERE a.log_curv_id = b.log_curv_id 
     AND b.lggg_tlstr_pass_id = 188481 
     AND a.log_curv_type_id = c.log_curv_type_id 
     AND NVL(a.cmpl_fac_id,0) = d.fac_id(+) 
     AND d.term_dttm IS NULL 
     AND a.del_indc = 'N' 
     AND b.del_indc = 'N' 
     AND c.del_indc = 'N' 
     AND d.fac_nme_type_cde = 'NME' /*12/09/2013 ksk NEW*/ 
     AND d.del_indc = 'N';   /*12/09/2013 ksk NEW*/ 

結果

enter image description here

你會發現,完成id是結果相同。我需要檢查Editedindicator列中的Y標誌,如果存在,請查找包含匹配的completionid並將其刪除的結果。

+0

遊標是隻讀結構,因此您無法對其進行修改。您可以將光標中的所有數據提取到本地集合或全局臨時表中,然後編寫一個查詢,然後根據需要修改數據,然後將其作爲單獨的遊標返回給調用者。但這遠非一個優雅的方法。我通常更願意修改原始過程來處理新的需求,或者編寫一個單獨的過程來查詢相同的表(可能將一些邏輯視爲視圖)或將第一個過程轉換爲流水線表函數。 –

+0

這是我能夠做到的事情,但我不知道甲骨文知道從哪裏開始。 – Brandon

+1

你可以縮小這個問題嗎?你問如何從遊標中獲取?如何寫入全局臨時表?如何創建全局臨時表?如何編寫將過程返回的數據轉換爲您想要的數據的查詢?如何以我建議的方式之一重構原始程序,以避免光標移到本地存儲到光標黑客?還有別的嗎? –

回答

2
with yq as (
select a.log_curv_id, a.log_curv_type_id, c.log_curv_type_desc, a.cmpl_fac_id, 
     d.fac_nme, b.edtd_curv_indc, b.stsfr_qlty_indc, a.top_md_qty, a.base_md_qty, 
     b.lggg_tlstr_pass_id PassId, 
     count (case when edtd_curv_indc = 'Y' then 1 end) 
     over (partition by b.lggg_tlstr_pass_id) cnt 
    from log_curv a 
    join acqn_curv b  on b.del_indc = 'N' and a.log_curv_id = b.log_curv_id 
    join log_curv_type c on c.del_indc = 'N' and a.log_curv_type_id = c.log_curv_type_id 
    left join fac_nme d on nvl(a.cmpl_fac_id,0) = d.fac_id and d.del_indc = 'N' 
          and d.term_dttm is null and d.fac_nme_type_cde = 'NME' 
    where b.lggg_tlstr_pass_id = 188481 and a.del_indc = 'N') 
select yq.* from yq where (cnt>0 and edtd_curv_indc='Y') or cnt = 0 

SQLFiddle demo

在上述我用解析count()功能,以檢查是否存在排,edtd_curv_indc = 'Y' 爲特定PassId查詢。 如果是這樣,只有這些行會顯示,如果沒有 - 行的其餘部分,根據條件where (cnt>0 and edtd_curv_indc='Y') or cnt = 0。 在答案中,我忽略了使用函數fget_log_curv_spec的列,它們對於此示例不重要。 我還將舊式連接更改爲ansi語法。