fibonacci系列程序使用pl/sql中的函数



我必须创建一个函数,将斐波那契数列打印为其结果。我在下面的程序中使用了varray,但它给了我一个错误,说";PLS-00201:标识符"ARRAY"必须声明为";在2号线上。

create function fibonacci7(x int)
return VARRAY
is
type fib IS VARRAY(25) OF VARCHAR(10);
a number(3):=1;
b number(3):=1;
c number(3);
i number(3):=1;
begin
while a<=n
loop
fib(i) := a;
c:=a+b;
a:=b;
b:=c;
i:=i+1;
end loop;
return codes_;
end ;
/
select fibonacci7(5) from dual;

我认为这将满足您的需求。VARRAY的语法与您使用的语法略有不同。

set serveroutput on size 1000000
create or replace type fibtype AS VARRAY(25) OF NUMBER;
/
create or replace function fibonacci7(n number)
return fibtype
is
fib fibtype := fibtype();
a number:=1;
b number:=1;
c number;
i number:=1;
begin
fib.extend(n);
while i<=n
loop
fib(i) := a;
c:=a+b;
a:=b;
b:=c;
i:=i+1;
end loop;
return fib;
end ;
/
declare
i number;
fib fibtype := fibtype();
begin
fib := fibonacci7(6);
for i in 1..fib.count loop
dbms_output.put_line(to_char(fib(i)));
end loop;
end;
/

这是输出。

1
1
2
3
5
8

Bobby

p.s.固定使用fib(6(并使数组编号为