Yes Stored Procedure call itself but
"Maximum stored procedure, function, trigger, or view nesting level should not be exceeded (limit 32)".
-------------------------------First Approach---------Through Function-------
create function FibnocciSeries(@number1 int, @number2 int, @count int)
returns @table table (i int)
as
begin
declare @number3 int
set @number3 = @number2 + @number1
set @count = @count-1
if @count <> 0
insert into @table
The best approach to recursion is to not do it in the first place. Recursion on a sufficiently large fibonacci number will consume too much memory, or CPU, etc. until the process crashes.
The better approach is to iterate over the fib series like an array, starting at zero. For large fib numbers you will also run into data type issues (the value is too large for an int or bigint to hold for example).
This blog post shows a pretty good approach that handles most of the common fibonacci pitfalls.
mehmoodahmed...
Member
1 Points
7 Posts
best approach to recursion
Jul 02, 2012 01:59 PM|LINK
Which one is best approach?
Yes Stored Procedure call itself but "Maximum stored procedure, function, trigger, or view nesting level should not be exceeded (limit 32)". -------------------------------First Approach---------Through Function------- create function FibnocciSeries(@number1 int, @number2 int, @count int) returns @table table (i int) as begin declare @number3 int set @number3 = @number2 + @number1 set @count = @count-1 if @count <> 0 insert into @tableDarrellNorto...
All-Star
86703 Points
9638 Posts
Moderator
MVP
Re: best approach to recursion
Jul 02, 2012 02:15 PM|LINK
The best approach to recursion is to not do it in the first place. Recursion on a sufficiently large fibonacci number will consume too much memory, or CPU, etc. until the process crashes.
The better approach is to iterate over the fib series like an array, starting at zero. For large fib numbers you will also run into data type issues (the value is too large for an int or bigint to hold for example).
This blog post shows a pretty good approach that handles most of the common fibonacci pitfalls.
http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/sql-server-fibonacci-sequence
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.