Din type:
create or replace TYPE INNUMARRAY AS TABLE OF INTEGER;
er en samling defineret i SQL-omfanget.
Dit beståede argument:
p_strings.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
p_strings.Value = new int[5]{1,2,3,4,5};
er et associativt array, som kun kan defineres i et PL/SQL-scope (dvs. i en pakke eller inden for en PL/SQL-blok) og ikke kan bruges i et SQL-scope.
Det er to forskellige og inkompatible datatyper.
I stedet kan du oprette en associativ array-type i en pakke og derefter manuelt udtrække hver værdi fra den associative array til en samling, der kan bruges i SQL-omfanget:
CREATE PACKAGE vehicles_pkg IS
TYPE INNUMASSOCARRAY IS TABLE OF INTEGER INDEX BY BINARY_INTEGER;
PROCEDURE v1
(
VehicleGroupID_Array IN INNUMASSOCARRAY
);
END;
/
CREATE PACKAGE BODY vehicles_pkg IS
PROCEDURE v1
(
VehicleGroupID_Array IN INNUMASSOCARRAY
)
IS
p_recordset SYS_REFCURSOR;
p_array INNUMARRAY := INNUMARRAY();
i BINARY_INTEGER;
BEGIN
i := VehicleGroupID_Array.FIRST;
WHILE i IS NOT NULL LOOP
p_array.EXTEND;
p_array( p_array.COUNT ) := VehicleGroupID_Array(i);
i := VehicleGroupID_Array.NEXT(i);
END LOOP;
-- Rest of your procedure using p_array instead of the associative array.
END;
END;
/
kan jeg definere den associative array-type uden for pakken? Jeg vil have dem til at være selvstændige.
Nej, men du kan definere en pakke, der kun indeholder typen:
CREATE PACKAGE vehicles_pkg IS
TYPE INNUMASSOCARRAY IS TABLE OF INTEGER INDEX BY BINARY_INTEGER;
END;
/
CREATE PROCEDURE v1
(
VehicleGroupID_Array IN vehicles_pkg.INNUMASSOCARRAY
)
IS
p_recordset SYS_REFCURSOR;
p_array INNUMARRAY := INNUMARRAY();
i BINARY_INTEGER;
BEGIN
i := VehicleGroupID_Array.FIRST;
WHILE i IS NOT NULL LOOP
p_array.EXTEND;
p_array( p_array.COUNT ) := VehicleGroupID_Array(i);
i := VehicleGroupID_Array.NEXT(i);
END LOOP;
-- Rest of your procedure using p_array instead of the associative array.
END;
/
Eller bedre, opret nogle generisk navngivne typer og en funktion i pakken for at oversætte fra et associativt array til en samling og derefter genbruge dem i dine procedurer:
SQL Fiddle
Oracle 11g R2 Schema Setup :
CREATE TYPE IntList AS TABLE OF INTEGER
/
CREATE PACKAGE tools IS
TYPE IntMap IS TABLE OF INTEGER INDEX BY BINARY_INTEGER;
FUNCTION IntMapToList(
i_map IntMap
) RETURN IntList;
END;
/
CREATE PACKAGE BODY tools IS
FUNCTION IntMapToList(
i_map IntMap
) RETURN IntList
IS
o_list IntList := IntList();
i BINARY_INTEGER;
BEGIN
IF i_map IS NOT NULL THEN
i := o_list.FIRST;
WHILE i IS NOT NULL LOOP
o_list.EXTEND;
o_list( o_list.COUNT ) := i_map( i );
i := i_map.NEXT( i );
END LOOP;
END IF;
RETURN o_list;
END;
END;
/
CREATE PROCEDURE v1
(
VehicleGroupID_Array IN tools.IntMap
)
IS
p_recordset SYS_REFCURSOR;
p_array IntList := tools.IntMapToList( VehicleGroupID_Array );
i BINARY_INTEGER;
BEGIN
-- Rest of your procedure using p_array instead of the associative array.
NULL;
END;
/