If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Recursivity

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-02-05, 07:34
dvtdbd dvtdbd is offline
Registered User
 
Join Date: Feb 2005
Location: Barcelona
Posts: 42
Question Recursivity


Hi,

I need to use recursivity in DB2 functions and procedures.

Example:
CREATE FUNCTION fibonacci(
number integer
)
RETURNS integer
NO EXTERNAL ACTION NOT DETERMINISTIC READS SQL DATA
BEGIN ATOMIC
IF number = 0 OR number = 1 THEN
RETURN 1;
ELSE
RETURN fibonacci(number - 1) + fibonacci(number - 2);
END IF;
END;

Execution result:
SQLSTATE 42884
When a function or procedure calls other routines, the creation order must be ascending, from the most sheltered one to which makes first call. But in recursivity case it enters conflict, due to the called routine, which must exist, is the same one that contains the call.

How can recursivity be used in DB2 functions or procedures?

Last edited by dvtdbd; 02-02-05 at 07:38.
Reply With Quote
  #2 (permalink)  
Old 02-02-05, 09:19
gardenman gardenman is offline
Registered User
 
Join Date: Apr 2004
Posts: 54
Quote:
Originally Posted by dvtdbd

Hi,

I need to use recursivity in DB2 functions and procedures.

Example:
CREATE FUNCTION fibonacci(
number integer
)
RETURNS integer
NO EXTERNAL ACTION NOT DETERMINISTIC READS SQL DATA
BEGIN ATOMIC
IF number = 0 OR number = 1 THEN
RETURN 1;
ELSE
RETURN fibonacci(number - 1) + fibonacci(number - 2);
END IF;
END;

Execution result:
SQLSTATE 42884
When a function or procedure calls other routines, the creation order must be ascending, from the most sheltered one to which makes first call. But in recursivity case it enters conflict, due to the called routine, which must exist, is the same one that contains the call.

How can recursivity be used in DB2 functions or procedures?
with tmp(next,prev,cnt) as (
values (1,0,0)
union all
select prev+next,next,cnt+1 from tmp
where cnt<10
) select * from tmp@


NEXT PREV CNT
----------- ----------- -----------
1 0 0
1 1 1
2 1 2
3 2 3
5 3 4
8 5 5
13 8 6
21 13 7
34 21 8
55 34 9
89 55 10
Reply With Quote
  #3 (permalink)  
Old 02-03-05, 04:47
dvtdbd dvtdbd is offline
Registered User
 
Join Date: Feb 2005
Location: Barcelona
Posts: 42
Angry Is recursivity possible?


Thanks gardernman for that fibonacci series alternative solution, but my question is:

Does DB2 allow recusrivity or I should change my functions and procedures (algorithms) from recursivity to iterativity?

Anybody can answer me?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On