Returning multiple values from an Oracle 12c function

此生再无相见时 提交于 2020-01-05 09:36:12

问题


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; 

回答1:


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


来源:https://stackoverflow.com/questions/26811959/returning-multiple-values-from-an-oracle-12c-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!