0

I want to execute a procedure which takes another procedure as an argument which has some other arguments or parameters. E.g.

ProcA(Proc_B(Name Varchar2, ID Varchar2))

Is this possible? If,so, please suggest me the possible solution.

2
  • The input parameter to a procedure has to be oracle defined datatype or user defined datatype. It cannot be another Procedure or package . So its not possible.
    – XING
    Jun 18, 2019 at 12:27
  • 1
    The nearest to this in PL/SQL would be to pass an instance of an object type. The object you pass could have attributes and methods which the procedure could use however it chooses. (On my phone at the moment or I’d post an example.) There is no built-in support for callback functions etc though. Jun 18, 2019 at 13:40

1 Answer 1

2

This is not possible.

A procedure does not directly return a value. This is different to a function that does return a value.

So you could do:

ProcedureA( FunctionB( name, id ) )

(Note: This is not passing a function as an argument but is passing the result of the function as an argument.)

Like this:

DECLARE
  FUNCTION FunctionB(
    name   IN  VARCHAR2,
    id     IN  NUMBER
  ) RETURN VARCHAR2
  IS
  BEGIN
    RETURN name || id;
  END;

  PROCEDURE ProcedureA(
    value IN VARCHAR2
  )
  IS
  BEGIN
    DBMS_OUTPUT.PUT_LINE( value );
  END;
BEGIN
  ProcedureA(
    FunctionB(
      name   => 'A',
      id     => 1
    )
  );
END;
/

An alternative would be to use an output parameter from ProcedureA and an intermediate variable:

DECLARE
  temp VARCHAR2(50);

  PROCEDURE ProcedureB(
    name   IN  VARCHAR2,
    id     IN  NUMBER,
    result OUT VARCHAR2
  )
  IS
  BEGIN
    result := name || id;
  END;

  PROCEDURE ProcedureA(
    value IN VARCHAR2
  )
  IS
  BEGIN
    DBMS_OUTPUT.PUT_LINE( value );
  END;
BEGIN
  ProcedureB(
    name   => :name,
    id     => :id,
    result => temp
  );
  ProcedureA( temp );
END;
/

But you cannot nest one procedure inside the call invoking another.

2
  • Thanks for your answer. Can you elaborate this method ProcedureA( FunctionB( name, id ) ) please by giving an example or something. It will be a huge help for me. Thanks.
    – rishabh
    Jun 18, 2019 at 12:34
  • 2
    @rishabh Example: dbms_output.put_line(upper('toast')); Jun 18, 2019 at 13:35

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.