I have 2 functions :
one in a package, one outside a package with a simple create function.
In my anonymous block, I try to call them :
declare
...
begin
my_variable := get_myfunction_outside_a_package();
my_other_variable := PK_myPackage.get_function_inside_a_package();
end;
This call would work inside a package.
Here, I have errors saying that those functions are not defined…
Is it possible to call those functions inside an anonymous block…How to do that ?
2
Your code works (if you define the variables, function and package):
CREATE FUNCTION get_myfunction_outside_a_package RETURN NUMBER
IS
BEGIN
RETURN 42;
END;
/
CREATE PACKAGE PK_myPackage
IS
FUNCTION get_function_inside_a_package RETURN NUMBER;
END;
/
CREATE PACKAGE BODY PK_myPackage
IS
FUNCTION get_function_inside_a_package RETURN NUMBER
IS
BEGIN
RETURN 6 * 9;
END;
END;
/
Then:
declare
my_variable NUMBER;
my_other_variable NUMBER;
begin
my_variable := get_myfunction_outside_a_package();
my_other_variable := PK_myPackage.get_function_inside_a_package();
DBMS_OUTPUT.PUT_LINE(my_variable);
DBMS_OUTPUT.PUT_LINE(my_other_variable);
end;
/
Outputs:
42
54
fiddle
my functions return associative arrays.
That works too.
Define the associative array in a package so it is available to be used:
CREATE PACKAGE pk_with_type
IS
TYPE assoc_array IS TABLE OF VARCHAR2(20) INDEX BY VARCHAR2(20);
END;
/
Then use it:
CREATE FUNCTION get_myfunction_outside_a_package
RETURN pk_with_type.assoc_array
IS
v_ary pk_with_type.assoc_array;
BEGIN
v_ary('X') := '42';
RETURN v_ary;
END;
/
CREATE PACKAGE PK_myPackage
IS
FUNCTION get_function_inside_a_package RETURN pk_with_type.assoc_array;
END;
/
CREATE PACKAGE BODY PK_myPackage
IS
FUNCTION get_function_inside_a_package RETURN pk_with_type.assoc_array
IS
v_ary pk_with_type.assoc_array;
BEGIN
v_ary('Y') := '6 * 9';
RETURN v_ary;
END;
END;
/
declare
my_variable pk_with_type.assoc_array;
my_other_variable pk_with_type.assoc_array;
begin
my_variable := get_myfunction_outside_a_package();
my_other_variable := PK_myPackage.get_function_inside_a_package();
DBMS_OUTPUT.PUT_LINE(my_variable('X'));
DBMS_OUTPUT.PUT_LINE(my_other_variable('Y'));
end;
/
(Note: The associative array could be defined in the same package as the package function.)
fiddle
4