要查看的存储过程
Posted
技术标签:
【中文标题】要查看的存储过程【英文标题】:Stored procedure to View 【发布时间】:2013-05-28 01:23:54 【问题描述】:我有 oracle 存储过程,它有三个输入参数和四个输出参数。存储过程查表很少,有一些逻辑,不是很复杂,但是根据不同的条件查了很多。另一个系统想要使用相同的逻辑。不幸的是,调用存储过程或将逻辑放入 SQL 对他们来说成本更高(人工天数)。他们希望访问视图或表并使用 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 是应替换为视图的过程。
【问题讨论】:
如果您可以将存储过程重写为 SELECT 语句,那么您可以将其转换为视图。如果你不能做到这一点,它就不能成为一个视图。例如,您的程序逻辑无法在 SELECT 语句中编写为内联逻辑,您无法将其创建为视图。您为什么不发布一些代码,我们可以提供帮助。 一些关于你的程序的核心、它的界面、他们如何检索数据的描述的细节在问题中很有用。 现在清理代码。将很快发布代码。谢谢各位 @Murali,昨天有一个"oposite question",也许它可以提供一些想法。我建议查看"parameterized view" 或“流水线函数” 【参考方案1】:您可以使用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
【讨论】:
以上是关于要查看的存储过程的主要内容,如果未能解决你的问题,请参考以下文章
sql server 2008,如何查看存储过程里面的内容?