如何在函数

时间:2016-11-07 18:42:43

标签: oracle plsql

现在,为了完全透明,这是家庭作业。第一部分是创建一个存储函数来计算项目的折扣价格。它接受项ID的一个参数并返回折扣价的值。函数名称为discount_price。我已经完成了它并且工作正常。

下一部分是:“5。编写一个脚本,创建并调用名为item_total的存储函数,计算Order_Items表中项目的总量(折扣价格乘以数量)。为此,此函数应该接受项目ID的一个参数,它应该使用您在练习2中创建的discount_price函数,它应该返回该项目的总计值。“

我的问题是,如何将一个函数的值传递给另一个函数?我只需要基本的语法。我的教科书中没有任何例子,我无法在任何地方找到明确的答案。

2 个答案:

答案 0 :(得分:4)

您可以完全按照从语句或查询中调用函数的方式调用函数内的函数;例如:

create function innerFunction(a number) return number is
begin
    return a * 2;
end;

create function outerFunction(a number) return number is
begin
    return innerFunction(a) * 3;
end;

create function calledFunction(a number) return number is
    n number;
begin
    n := outerFunction(a) * 5;
    return n;
end;

SQL> select calledFunction(1) from dual;

CALLEDFUNCTION(1)
-----------------
               30

SQL> select calledFunction(calledFunction(calledFunction(1))) from dual;

CALLEDFUNCTION(CALLEDFUNCTION(CALLEDFUNCTION(1)))
-------------------------------------------------
                                            27000


SQL> declare
  2  x number;
  3  begin
  4  x := calledFunction(1);
  5  dbms_output.put_line(x);
  6  end;
  7  /
30

答案 1 :(得分:1)

相信this link有你正在寻找的例子。 基本上你称之为就像你通常在sql-plus或sql-developer中调用它一样。

例如:

  

returl_val := SUBSTR(string_in,4,1);

相关问题