2013-05-28 43 views
1

我有oracle存儲過程,它有三個輸入參數和四個輸出參數。存儲過程查找幾個表,並有一些邏輯,不是很複雜,但根據不同的條件查找很多。另一個系統要使用相同的邏輯。不幸的是,調用一個存儲過程或將邏輯放入SQL中對他們來說代價高昂(人工天)。他們想訪問一個View或者一個表格,並使用where子句過濾器來獲得結果。是否有任何模式或建議的方法來實現這一點?存儲過程查看

讓我知道你是否需要更多信息。

我可以將存儲過程更改爲函數或任何需要但不能更改接口系統。這是一些代碼。

CREATE OR REPLACE PACKAGE BODY PKG_TEST 
IS 
    FUNCTION F_DETERMINE_SOMETHING 
    (
     i_location_id IN T_SYSTEM_LOCATION.SYSTEM_LOCATION_ID%TYPE, 
     i_customer_id IN T_CUSTOMER.CUSTOMER_ID%TYPE, 
     i_ccy IN T_CURRENCY.CCY_ID%TYPE 
    ) 
    RETURN RC_SOME_ACCT_INFO 
    IS 
     o_some_acct_info RC_SOME_ACCT_INFO; 
     CURSOR some_acct_cursor IS 
     select some_acct_id, 
       area, 
       portfolio 
     from t_override_some_acct 
     where customer_id = i_customer_id 
      and SYSTEM_location_id = i_location_id 
      and ccy1_id = i_ccy; 
    BEGIN 
     OPEN some_acct_cursor; 
     FETCH some_acct_cursor into o_some_acct_info.tdr_id, o_some_acct_info.bk_area, o_some_acct_info.bk_portfolio; 
     CLOSE some_acct_cursor; 

     return o_some_acct_info; 
    END F_DETERMINE_SOMETHING; 


    FUNCTION F_GET_SOME_ACCT_GRT 
    (
     i_location_id IN T_SYSTEM_LOCATION.SYSTEM_LOCATION_ID%TYPE, 
     i_ccy IN T_CURRENCY.CCY_ID%TYPE 
    ) 
    RETURN RC_SOME_ACCT_INFO 
    IS 
     o_some_acct_info RC_SOME_ACCT_INFO; 

     CURSOR SYSTEM_location_accounts_cursor IS 
     select risk_account, 
     risk_area 
     from V_SYSTEM_LOCATION_ACCTS 
     where SYSTEM_location_id = i_location_id 
     and ccy1_id = i_ccy; 

     CURSOR SYSTEM_location_traderId_cursor IS 
     select autopric_tdr_id 
     from V_SYSTEM_LOCATION 
     where SYSTEM_location_id = i_location_id; 

    BEGIN 
     OPEN SYSTEM_location_accounts_cursor; 
     FETCH SYSTEM_location_accounts_cursor into o_some_acct_info.bk_portfolio, o_some_acct_info.bk_area; 
     CLOSE SYSTEM_location_accounts_cursor; 

     OPEN SYSTEM_location_traderId_cursor; 
     FETCH SYSTEM_location_traderId_cursor into o_some_acct_info.tdr_id; 
     CLOSE SYSTEM_location_traderId_cursor; 

     return o_some_acct_info; 

    END F_GET_SOME_ACCT_GRT; 


    FUNCTION F_DETERMINE_SOME_ACCT 
    (
     i_location_id IN T_SYSTEM_LOCATION.SYSTEM_LOCATION_ID%TYPE, 
     i_customer_id IN T_CUSTOMER.CUSTOMER_ID%TYPE, 
     i_bought_ccy IN T_CURRENCY.CCY_ID%TYPE, 
     i_sold_ccy IN T_CURRENCY.CCY_ID%TYPE 
    ) 
    RETURN RC_SOME_ACCT_INFO 
    IS 
     o_some_acct_ids RC_SOME_ACCT_INFO; 
     v_SYSTEM_location_id T_SYSTEM_LOCATION.SYSTEM_LOCATION_ID%TYPE; 

    BEGIN 
     v_SYSTEM_location_id := i_location_id; 

     if (v_SYSTEM_location_id <> 4) then 
     begin 
      o_some_acct_ids := F_DETERMINE_SOMETHING(v_SYSTEM_location_id, i_customer_id, i_bought_ccy); 

      if (o_some_acct_ids.bk_area is null OR o_some_acct_ids.bk_portfolio is null) then 
      begin 
       o_some_acct_ids := F_DETERMINE_SOMETHING(v_SYSTEM_location_id, i_customer_id, i_sold_ccy); 

       if (o_some_acct_ids.bk_area is null OR o_some_acct_ids.bk_portfolio is null) then 
       begin 
        o_some_acct_ids := F_DETERMINE_SOMETHING(v_SYSTEM_location_id, i_customer_id, PKG_CONSTANTS.C_OTH_VALUE); 

         if (o_some_acct_ids.bk_area is null OR o_some_acct_ids.bk_portfolio is null) then 
         begin 
          v_SYSTEM_location_id := PKG_CONSTANTS.C_SYSTEM_LOCATION_ALL; 

          o_some_acct_ids := F_DETERMINE_SOMETHING(v_SYSTEM_location_id, i_customer_id, i_bought_ccy); 

          if (o_some_acct_ids.bk_area is null OR o_some_acct_ids.bk_portfolio is null) then 
          begin 
           o_some_acct_ids := F_DETERMINE_SOMETHING(v_SYSTEM_location_id, i_customer_id, i_sold_ccy); 

           if (o_some_acct_ids.bk_area is null OR o_some_acct_ids.bk_portfolio is null) then 
           begin 
            o_some_acct_ids := F_DETERMINE_SOMETHING(v_SYSTEM_location_id, i_customer_id, PKG_CONSTANTS.C_OTH_VALUE); 
           end; 
           end if; 
          end; 
          end if; 
         end; 
         end if; 
       end; 
       end if; 
      end; 
      end if; 
     end; 
     else 
     o_some_acct_ids := F_GET_SOME_ACCT_GRT(v_SYSTEM_location_id, i_bought_ccy); 
     if (o_some_acct_ids.bk_area is null OR o_some_acct_ids.bk_portfolio is null) then 
     begin 
      o_some_acct_ids := F_GET_SOME_ACCT_GRT(v_SYSTEM_location_id, i_sold_ccy); 
      if (o_some_acct_ids.bk_area is null OR o_some_acct_ids.bk_portfolio is null) then 
      begin 
       o_some_acct_ids := F_GET_SOME_ACCT_GRT(v_SYSTEM_location_id, PKG_CONSTANTS.C_OTH_VALUE); 
      end; 
      end if; 
     end; 
     end if; 
     end if; 
     return o_some_acct_ids; 
    END F_DETERMINE_SOME_ACCT; 

    PROCEDURE P_RETRIEVE_SYSM_CUST_DETAILS (
     i_sysm_short_name IN T_CUSTOMER.SYSM_CUSTOMER_ID%TYPE, 
     i_sysm_legal_entity IN T_SYSM_LEI_LOCATION_MAPPING.SYSM_LEGAL_ENTITY%TYPE, 
     i_bought_ccy IN T_CURRENCY.CCY_ID%TYPE, 
     i_sold_ccy IN T_CURRENCY.CCY_ID%TYPE, 
     o_sysw_customer_id OUT T_CUSTOMER.SYSW_CUST_ID%TYPE, 
     o_sysw_city OUT T_CUSTOMER.CITY%TYPE, 
     o_tdr_id OUT T_OVERRIDE_SOME_ACCT.SOME_ACCT_ID%TYPE, 
     o_bk_area OUT T_OVERRIDE_SOME_ACCT.AREA%TYPE, 
     o_bk_portfolio OUT T_OVERRIDE_SOME_ACCT.PORTFOLIO%TYPE, 
     o_error OUT varchar2 
    ) 
    IS 
     v_SYSTEM_location_id T_SYSTEM_LOCATION.SYSTEM_LOCATION_ID%TYPE; 
     v_customer_id T_CUSTOMER.CUSTOMER_ID%TYPE; 
     v_city T_CUSTOMER.CITY%TYPE; 
     v_sysw_cust_id T_CUSTOMER.SYSW_CUST_ID%TYPE; 

     o_some_acct_ids RC_SOME_ACCT_INFO; 

     CURSOR customer_cursor IS 
     select customer_id, sysw_cust_id, city 
     from v_customer 
     where sysm_customer_id = UPPER(i_sysm_short_name); 

     CURSOR lei_location_cursor IS 
     select location_id 
     from T_SYSM_LEI_LOCATION_MAPPING 
     where sysm_legal_entity = UPPER(i_sysm_legal_entity); 
    BEGIN 
     open lei_location_cursor; 
     FETCH lei_location_cursor into v_SYSTEM_location_id; 
     close lei_location_cursor; 

     open customer_cursor; 
     FETCH customer_cursor into v_customer_id,v_sysw_cust_id,v_city; 
     close customer_cursor; 
     o_some_acct_ids := F_DETERMINE_SOME_ACCT(v_SYSTEM_location_id, v_customer_id, i_bought_ccy, i_sold_ccy); 

     if (o_some_acct_ids.bk_area is null OR o_some_acct_ids.bk_portfolio is null) then 
      o_error := '<<Error: Data not found>>'; 
     else 
      o_sysw_customer_id := v_sysw_cust_id; 
      o_sysw_city := v_city; 
      o_tdr_id := o_some_acct_ids.tdr_id; 
      o_bk_area := o_some_acct_ids.bk_area; 
      o_bk_portfolio := o_some_acct_ids.bk_portfolio; 
      o_error := null; 
     end if; 
    END P_RETRIEVE_SYSM_CUST_DETAILS; 
END PKG_TEST; 

P_RETRIEVE_SYSM_CUST_DETAILS是應該用視圖替換的過程。

+0

如果你可以重寫你的存儲過程作爲一個SELECT語句,那麼你可以將其轉換爲一個視圖。如果你不能這樣做,它不能成爲一個觀點。例如,你有一個程序邏輯不能在SELECT語句中作爲內聯邏輯寫入,你不能將它創建爲視圖。你爲什麼不發佈一些代碼,我們可以提供幫助。 –

+0

有關你的程序,它的接口,他們如何檢索數據的內容的一些細節將有助於在問題中有所幫助。 –

+0

立即清理代碼。將很快發佈代碼。謝謝你 – Murali

回答

3

你可以用pipelined function包裝你的程序。

這可能是像這個 -

create type t_out_params as object 
(
    o_1 number, 
    o_2 number, 
    o_3 number, 
    o_4 number 
); 

create type t_out_params_tab is table of t_out_params; 

create function f(i_1 number, i_2 number, i_3 number) return t_out_params_tab pipelined as 
begin 
    -- in this example I just calculated things with the input parameters 
    -- you can run your logic or stored procedure and get the 4 out parameters 
    -- instead (should initialize t_out_params) 
    pipe row(t_out_params(i_1*2, i_2+10, i_3, i_1+i_2+i_3)); 
return; 
end; 

Here is a sqlfiddle example