简体   繁体   中英

Returning multiple values from an Oracle 12c function

I am writing a function in Oracle 12c (and it has to be a function) that should return 3 values, order_line.o_id , a variable total that I create in the function, and a discounted_amount variable that I create in the function.

I have gotten my function to create the discounted_amount variable and return it but I am not sure how to get it to return these other two values.

CREATE OR replace FUNCTION DiscountsReport (input_o_id IN REAL) 
RETURN REAL 
IS 
  output_o_id      NUMBER; 
  total            REAL; 
  discounted_total REAL; 
  percent          REAL; 
BEGIN 
    output_o_id := input_o_id; 

    SELECT SUM(o.ol_quantity * o.ol_price) 
    INTO   total 
    FROM   order_line o 
    WHERE  o.o_id = input_o_id; 

    IF total > 100 THEN 
      percent := 0.1; 
    ELSIF total > 200 THEN 
      percent := 0.2; 
    ELSE 
      percent := 0.0; 
    END IF; 

    discounted_total := ( total * ( 1 - percent ) ); 

    RETURN discounted_total; 
END; 

Create a new type:

CREATE OR REPLACE TYPE new_type AS OBJECT(v1 type1, v2 type2, v3 type3);

and use it after RETURN (call the result output - its type is new_type ).

You can access those values using:

output.v1
output.v2
output.v3

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