简体   繁体   English

试图在 oracle 的存储过程中调用 Function

[英]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.我正在尝试从 Oracle 中的存储过程调用 function,但不知道该怎么做。 my function has two IN parameter and one OUT parameter.我的 function 有两个 IN 参数和一个 OUT 参数。 in my procedure i am using out sys refcursor.在我的过程中,我正在使用 out sys refcursor。 Any refrence or example will help me a lot.任何参考或示例都会对我有很大帮助。

Here is a simple example for calling function inside procedure.这是一个在过程中调用 function 的简单示例。 Also as mentioned by APC using OUT in function is a bad practice.正如 APC 提到的,在 function 中使用OUT是一种不好的做法。 Instead you can return your required output.相反,您可以return所需的 output。 And I'm not sure how you are using sys_refcursor , so modify your procedure accordingly而且我不确定您如何使用sys_refcursor ,因此请相应地修改您的程序

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.一般来说,我没有看到太多将 OUT 参数与函数一起使用的代码,如果您想将多个值返回给调用者 object,那么您可以使用具有多个 OUT 变量的过程。 There are some limitation on how an oracle function with OUT parameter would differ from a normal function.带有 OUT 参数的 oracle function 与正常的 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;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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