Search code examples
oracleplsql

How to call a function from an anonymous PLSQL block


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 ?


Solution

  • 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