Sử dụng Oracle để làm các biểu thức toán học? Tại sao không nhỉ
Hôm nay chúng ta sẽ thử in ra dãy số fibonacci bằng SQL trên Oracle nhé.
Trước tiên, nhắc lại về quy tắc tạo ra dãy số fibonacci

Để cài đặt trong Oracle, chúng ta hãy tạo ra function sau:
SQL> create or replace
2 function fibonacci(n int) return number is
3 n1 int := 1;
4 n2 int := 1;
5 n3 int;
6 begin
7 if n = 1 then return n1; end if;
8 if n = 2 then return n2; end if;
9 for i in 3 .. n
10 loop
11 n3 := n1 + n2;
12 n1 := n2;
13 n2 := n3;
14 end loop;
15 return n3;
16 end;
17 /
Function created.
Và hãy test thử function này:
SQL> select fibonacci(level)
2 from dual
3 connect by level <= 10;
FIBONACCI(LEVEL)
----------------
1
1
2
3
5
8
13
21
34
55
10 rows selected.
Toán học và SQL cũng thật thú vị đó
Leave a Review