sofiDev img

Mobashshir Hasan

Forward Declaration.

javascript image

🔹 What is Forward Declaration in Oracle (PL/SQL)?

Forward declaration means declaring a procedure or function before defining it, so that it can be used earlier in the program. It is mainly used when procedures call each other or when we want to define code in a different order..

It is like saying:

“Don’t worry, I will define this procedure later. For now, just know that it exists.”


Why Do We Need Forward Declaration?

Normally in PL/SQL:

A procedure/function must be defined before it is used.

But sometimes:

Two procedures call each other.

Or you want to organize code in a different order.

In that case, Oracle will throw an error unless you declare it first.

That’s where forward declaration helps.


🔹 Basic Example (Without Forward Declaration – Error)

DECLARE
   PROCEDURE proc1 IS
   BEGIN
      proc2;  -- calling proc2 before defining
   END;

   PROCEDURE proc2 IS
   BEGIN
      DBMS_OUTPUT.PUT_LINE('Hello');
   END;
BEGIN
   proc1;
END;

❌ This gives error because proc2 is not known at the time proc1 is compiled.


✅ Example With Forward Declaration (Correct Way)

DECLARE
   PROCEDURE proc2;  -- Forward Declaration

   PROCEDURE proc1 IS
   BEGIN
      proc2;  
   END;

   PROCEDURE proc2 IS
   BEGIN
      DBMS_OUTPUT.PUT_LINE('Hello');
   END;
BEGIN
   proc1;
END;