警告:执行已完成,但有警告 FUNCTION

Warning: execution completed with warning FUNCTION

我正在尝试创建一个函数来计算斐波那契数列的第 N 个元素。

CREATE OR REPLACE FUNCTION fib_mohamed_h(x number)
RETURN number IS
  f number;
DECLARE
  num number;
  fibbonnacci number
BEGIN
  IF (x<3) THEN
    f := 1;
  ELSE
    f := fib_mohamed_h(x-1)+fib_mohamed_h(x-2);
  END IF;
  RETURN f;
  x:= 3;
  fibbonnacci := fib_mohamed_h(x);
  dbms_output.put_line(' Fibbonnacci '|| x || ' is ' || fibbonnacci);
END;

这得到:

Warning: execution completed with warning FUNCTION fib_mohamed_h(x Compiled.

我做错了什么?

您似乎已将函数声明和函数调用合并到一个无效块中。它看起来几乎像一个剪切和粘贴错误。按照你的逻辑,函数应该是:

CREATE OR REPLACE FUNCTION fib_mohamed_h(x number)
RETURN number IS
 f number;
BEGIN
  IF (x<3) THEN
    f := 1;
  ELSE
    f := fib_mohamed_h(x-1)+fib_mohamed_h(x-2);
  END IF;
  RETURN f;
END;
/

电话可能是:

set serveroutput on size unlimited
DECLARE
  x number;
  fibbonnacci number;
BEGIN
  x:= 3;
  fibbonnacci := fib_mohamed_h(x);
  dbms_output.put_line(' Fibbonnacci '|| x || ' is ' || fibbonnacci);
END;
/

anonymous block completed
 Fibbonnacci 3 is 2

或从普通 SQL:

SELECT fib_mohamed_h(7) FROM dual;

FIB_MOHAMED_H(7)
----------------
              13 

SQL Fiddle demo.

Read more about how to create functions.