PLS-00382在PL / SQL函数中使用UDT时

时间:2018-03-21 00:26:22

标签: sql oracle plsql oracle12c

我正在尝试在Oracle上创建一个包。这个包有两个函数(我需要创建两个函数,我不能将它们联合起来)但是我在行上有这个错误:

mD(1) := rel_mahalanobis_distance_aux_3( A1,
B1,B2,B3);
x_minus_y(2) := ABS(A2 - B2);
mD(2) := rel_mahalanobis_distance_aux_3( A2,
B1,B2,B3);
x_minus_y(3) := ABS(A3 - B3);
mD(3) := rel_mahalanobis_distance_aux_3( A3,
B1,B2,B3);

错误:

  

PLS-00382:表达式类型错误

行上的错误:

mD(1) := rel_mahalanobis_distance_aux_3( A1,
B1,B2,B3);
x_minus_y(2) := ABS(A2 - B2);
mD(2) := rel_mahalanobis_distance_aux_3( A2,
B1,B2,B3);
x_minus_y(3) := ABS(A3 - B3);
mD(3) := rel_mahalanobis_distance_aux_3( A3,
B1,B2,B3);

包裹代码

CREATE PACKAGE mahalanobis_distance_package AS
max_d NUMBER;
TYPE tbNumber IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
function rel_mahalanobis_distance_aux_3( A NUMBER,B1 NUMBER,B2 NUMBER,B3 NUMBER)
RETURN  tbNumber;function rel_mahalanobis_distance_3(
A1 NUMBER, A2 NUMBER, A3 NUMBER,
B1 NUMBER,B2 NUMBER,B3 NUMBER) RETURN NUMBER ;
END mahalanobis_distance_package;
/
CREATE OR REPLACE PACKAGE BODY mahalanobis_distance_package AS
function rel_mahalanobis_distance_aux_3( A NUMBER,
B1 NUMBER,B2 NUMBER,B3 NUMBER) RETURN  tbNumber IS
tbNumber_obj tbNumber;
BEGIN
max_d := 0;
FOR j IN 1..3 LOOP
tbNumber_obj(j) := 0;
END LOOP;
tbNumber_obj(1) := ABS(A - B1);
IF tbNumber_obj(1) > max_d THEN
max_d := tbNumber_obj(1);
END IF;
tbNumber_obj(2) := ABS(A - B2);
IF tbNumber_obj(2) > max_d THEN
max_d := tbNumber_obj(2);
END IF;
tbNumber_obj(3) := ABS(A - B3);
IF tbNumber_obj(3) > max_d THEN
max_d := tbNumber_obj(3);
END IF;
return tbNumber_obj;
END rel_mahalanobis_distance_aux_3;
function rel_mahalanobis_distance_3(
A1 NUMBER, A2 NUMBER, A3 NUMBER,
B1 NUMBER,B2 NUMBER,B3 NUMBER) RETURN NUMBER IS
total NUMBER := 0;
aux NUMBER;
TYPE tbNumber IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
TYPE tbNumberMatrix IS TABLE OF tbNumber INDEX BY PLS_INTEGER;
mA tbNumberMatrix;
mD tbNumberMatrix;
x_minus_y INTEGER_ARRAY := INTEGER_ARRAY();
BEGIN
x_minus_y.extend(3) ;
FOR i IN 1..3 LOOP
FOR j IN 1..3 LOOP
mA(i)(j) := 0;
mD(i)(j) := 0;
END LOOP;
END LOOP;
x_minus_y(1) := ABS(A1 - B1);


mD(1) := rel_mahalanobis_distance_aux_3( A1,
B1,B2,B3);
x_minus_y(2) := ABS(A2 - B2);
mD(2) := rel_mahalanobis_distance_aux_3( A2,
B1,B2,B3);
x_minus_y(3) := ABS(A3 - B3);
mD(3) := rel_mahalanobis_distance_aux_3( A3,
B1,B2,B3);
FOR i IN 1..3 LOOP
FOR j IN 1..3 LOOP
mA(i)(j) := 1 - (mD(i)(j)/max_d);
END LOOP;
END LOOP;
FOR i IN 1..3 LOOP
aux := 0;
FOR j IN 1..3 LOOP
aux := aux + x_minus_y(j) * mA(j)(i);
END LOOP;
total := total + x_minus_y(i)*aux;
END LOOP;
RETURN  POWER(total,0.5);
END rel_mahalanobis_distance_3;
END mahalanobis_distance_package;
/

数据类型似乎是正确的。 我能做些什么才能让它发挥作用?

提前致谢!

1 个答案:

答案 0 :(得分:0)

问题在于函数rel_mahalanobis_distance_3

中的这一行
TYPE tbNumber IS TABLE OF NUMBER INDEX BY PLS_INTEGER;

您已经在包sepcification(全局范围)中声明了tbNumber,并且您正在重新声明它,这使得它在执行赋值时不兼容

删除该行,您的错误消息应该消失。

另外,我在代码中看不到类型INTEGER_ARRAY的声明,希望您使用可用的TYPE或在某处声明它。