简体   繁体   中英

Trying to call a Function inside a stored procedure in oracle

i am trying to call a function from stored procedure in Oracle, but not getting any idea how to do. my function has two IN parameter and one OUT parameter. in my procedure i am using out sys refcursor. Any refrence or example will help me a lot.

Here is a simple example for calling function inside procedure. Also as mentioned by APC using OUT in function is a bad practice. Instead you can return your required output. And I'm not sure how you are using sys_refcursor , so modify your procedure accordingly

CREATE OR REPLACE FUNCTION SUM_OF_2(NUM1 IN NUMBER,NUM2 IN NUMBER) RETURN NUMBER
 IS
    RESULT_SUM NUMBER;
    BEGIN
       RESULT_SUM:=NUM1+NUM2;
       RETURN RESULT_SUM;
    END;



CREATE OR REPLACE PROCEDURE CALL_FUNCTON(NUM1 NUMBER,NUM2 NUMBER)
AS
    V_FINAL_RESULT NUMBER;
    BEGIN
        V_FINAL_RESULT:=SUM_OF_2(NUM1,NUM2);
        DBMS_OUTPUT.PUT_LINE(V_FINAL_RESULT);
    END;

BEGIN
 CALL_FUNCTON(5,10);
END;
/

CHECK DEMO HERE

Not sure on what your requirement is, maybe you are just trying the code for education purposes. Generally I have not seen much code which uses OUT parameter with functions, in case you want to return multiple values to the caller object then you could use a procedure with more then one OUT variables. There are some limitation on how an oracle function with OUT parameter would differ from a normal function.

CREATE OR REPLACE FUNCTION temp_demo_func(out_var1 OUT NUMBER)
   RETURN VARCHAR2 IS
BEGIN
   out_var1 := 1;
   RETURN 'T';
EXCEPTION
   WHEN OTHERS THEN
      RETURN 'F';
END temp_demo_func;
/



CREATE OR REPLACE PROCEDURE temp_demo_proc
(
   in_var1        NUMBER
  ,cur_refcur_out OUT SYS_REFCURSOR
) IS
   res      VARCHAR2(1);
   out_var1 NUMBER;

BEGIN

   res := temp_demo_func(out_var1 => out_var1);

   dbms_output.put_line(out_var1);

   OPEN cur_refcur_out FOR
      SELECT in_var1
            ,out_var1
            ,res
        FROM dual;

END;
/


set serveroutput on
declare
cur_refcur_out  Sys_Refcursor;
in_var1 number := 22;
begin 

temp_demo_proc(in_var1 => in_var1
               ,cur_refcur_out => cur_refcur_out);

end;
/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM