--
-- ### Fibonacci calculation as demonstration for recursive procedure call  
-- 
--

drop if exists table fibNumber;
create table fibNumber(f int);


drop if exists procedure calcFibonacci;
@
create procedure calcFibonacci(n in int) return int
begin
	var f int;
	var c int;
	if :n > 2
	then
		:f = calcFibonacci( :n - 1 ) + calcFibonacci( :n - 2 );	
	else
		:f = 1;
	end;

	if ( select count(*) from fibNumber where f = :f ) = 0
	then
		insert into fibNumber values ( :f );
		if :f = 1
		then
			insert into fibNumber values ( :f );
		end;
	end;
	return  :f;

end;
@

call calcFibonacci(10);

select * from fibNumber;
