我想要一个函数为我的程序返回多个值。所以我编写了一个以记录类型返回值的函数。我可以毫无问题地编译该包。当从代码调用时它也运行良好。但是当我使用匿名块来检索函数返回的值时,我收到错误。有什么问题吗?
[错误]执行(60:20):ORA-06550:第11行,第20列:PLS-00382:表达式的类型错误ORA-06550:第11行,第3列:PL/SQL:忽略语句
封装规格
CREATE OR REPLACE package cust_wfm_budget
is
TYPE cap_details_rec IS RECORD (
cap_month VARCHAR2(55),
overcap_per NUMBER,
undercap_per NUMBER
);
function get_cap_month_fringes (
p_sal_distribution_id IN NUMBER,
p_cap_limit IN NUMBER
)
return cap_details_rec;
end cust_wfm_budget;
封装体
CREATE OR REPLACE package body cust_wfm_budget
is
l_cap_details cap_details_rec;
FUNCTION get_cap_month_fringes (
p_sal_distribution_id IN NUMBER,
p_cap_limit IN NUMBER
)
RETURN cap_details_rec
IS
lv_return VARCHAR2(55):='TEST'; -- Keep as string
l_overcap_per NUMBER := 0; -- Assign numeric values directly
l_undercap_per NUMBER := 0;
BEGIN
-- Replace this logic with actual calculations for overcap_per and undercap_per
-- l_overcap_per := /* Calculate overcap percentage */;
-- l_undercap_per := /* Calculate undercap percentage */;
RETURN cap_details_rec(lv_return, l_overcap_per, l_undercap_per);
END get_cap_month_fringes;
end cust_wfm_budget;
/
犯罪;
匿名区块
DECLARE
TYPE cap_details_rec IS RECORD (
cap_month VARCHAR2(55),
overcap_per NUMBER,
undercap_per NUMBER
);
l_cap_details cap_details_rec;
BEGIN
l_cap_details := cust_wfm_budget.get_cap_month_fringes(1, 1);
-- Do something with l_cap_details
END;
问题是您没有使用与您的包期望的类型相同的类型。仅仅因为您在匿名块中声明的类型与包中的类型具有相同的结构,并不意味着您可以互换它们。
相反,您需要做的就是使用您在包规范中声明的类型,而不是创建新类型,例如:
这在db<>fiddle中得到了演示。