-1

If I have procedure Proc1 and another Procedure Proc2 where proc1 depends on proc2 and proc2 depends on proc1 . I need to compile package pkg1 with both these procedures .

Oracile 9i. How can I achieve it? any particular keyword is required ?

4
  • You need to seperate the functionality out so that the objects don't have such circular dependency. Dec 12, 2014 at 2:43
  • I need to have the interdependent procedures. I hope i can compile as allan commented , but wanted to check if i need to use any other commands/keywords Dec 12, 2014 at 3:10
  • Technically, you should be able to compile as Allan suggested, there are options to do this at various levels (compile schema objects, for example), but this design will be confusing for some dependency-based scripts. Dec 12, 2014 at 3:19
  • If both are included in the package spec you should be fine. If not, I think you can include a dummy definition, e.g. PROCEDURE PROC2(SOME_ARG NUMBER); at the top of the package body so that PROC1 can see the definition of PROC2 (assuming PROC1 appears first in the package body). Best of luck. Dec 12, 2014 at 3:52

3 Answers 3

3

I presume these are both private procedures, that is, neither appears in the package specification.

If both procedures appeared in the spec there would be no problem, because the body compiles against the public declaration.

Likewise if one procedure appeared in the spec then again there would be no problem: simply write the private procedure in the body before the public procedure.

But if both procedures are private, then you need to use forward declaration. This simply means declaring the procedures' signatures at the top of the package. Exactly like putting them in the spec, only private. Here is a spec ...

create or replace package pkg as
    procedure main (n0 in out number);
end;
/

... and body ...

create or replace package body pkg as
    -- forward declarations
    procedure p1 (n1 in out number);
    procedure p2 (n2 in out number);

    -- actual declarations    
    procedure p1 (n1 in out number)
    is
    begin
        dbms_output.put_line('P1='||n1);
        if n1 < 3 then
            p2(n1);
        end if;
    end p1;
    procedure p2 (n2 in out number)
    is
    begin
        dbms_output.put_line('P2='||n2);
        n2 := n2+1;
        if n2 < 3 then
            p1(n2);
        end if;
    end p2;

    procedure main (n0 in out number)
    is
    begin
        if n0 < 0 then 
            p1(n0);
        else
            p2(n0);
        end if;
    end main;

end pkg;
/

"I need to have the interdependent procedures"

You really should try to figure out a way to avoid cyclic dependencies. It is a very bad programming practice. It is hard to understand how the procedures interrelate, and we have to manage the recursion and ensure that both procedures will definitely exit eventually without calling the other. Remember, this is not just a problem you have: it's a legacy you will pass one to future maintainers of your code.

0

You can break the dependency chain by using packages and compiling all your package specs prior to compiling package bodies. The specs will all compile without regard to dependencies and then when the bodies are compiled they can do so in any order as they will reference the already compiled specifications. Typically, you'll have package specs stored with a *.pks suffix and bodies with *.pkb suffix and some sort of build script with compiles them. Like so

/* Master Package Build Script */

-- Specs
@package_a.pks
@package_b.pks
@package_c.pks

-- Bodies
@package_a.pkb
@package_b.pkb
@package_c.pkb
-1

If both procedures are in the specification, simply compiling the package should work:

alter package your_package compile;

This assumes that your package is valid, of course.

If either (or both) procedure is not in the specification, the best thing to do is list it as a forward declaration at the beginning of the package body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.