How to get two return value from Oracle Stored Procedure

后端 未结 4 1108
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 07:39

I know how to get one return value from Oracle SP in Oracle as follow

MyReturn := MY_ORACLE_SP ();

If MY_ORACLE_SP2\'s ret

相关标签:
4条回答
  • 2020-12-19 07:43

    Try the below code I just modified the response from user Benoit

    ab=`sqlplus -s system/password << eof
    
    SET SERVEROUTPUT ON
    set pagesize 0;
    set heading off;
    set feedback off;
    set linesize 5000;
    set trimspool on;
    declare
       foo number := 30;
       bar number := 0;
    begin
       f(5,foo,bar);
       dbms_output.put_line(foo || ' ' || bar);
    end;
    /
    
    eof`
    
    echo $ab
    
    0 讨论(0)
  • 2020-12-19 07:48

    What you have there is technically not a procedure, but a function -- the difference being that a procedure does not have a return value and cannot be used as the right-hand-side of an assignment statement.

    You basically have two options:

    (1) Use OUT parameters. In this case I would make it a procedure with two OUT parameters. Generally people don't like functions that also have OUT parameters, as it violates the usual expectations. @Benoit's answer shows this method.

    (2) Define a type that contains multiple values and use this as the return type of the function. Example:

    CREATE TYPE two_values AS object (
      A NUMBER,
      b number
      );
      /
    
    CREATE FUNCTION get_two_values RETURN two_values AS
    BEGIN
      RETURN two_values(2,4);
    END;
    /
    
    0 讨论(0)
  • 2020-12-19 07:54

    Use OUTPUT parameters instead of the return value.

    0 讨论(0)
  • 2020-12-19 08:03
    -- IN arguments : you get them. You can modify them locally but caller won't see it
    -- IN OUT arguments: initialized by caller, already have a value, you can modify them and the caller will see it
    -- OUT arguments: they're reinitialized by the procedure, the caller will see the final value.
    CREATE PROCEDURE f (p IN NUMBER, x IN OUT NUMBER, y OUT NUMBER)
    IS
    BEGIN
       x:=x * p;
       y:=4 * p;
    END;
    /
    
    SET SERVEROUTPUT ON
    
    declare
       foo number := 30;
       bar number := 0;
    begin
       f(5,foo,bar);
       dbms_output.put_line(foo || ' ' || bar);
    end;
    /
    

    outputs: 150 20

    0 讨论(0)
提交回复
热议问题