473,385 Members | 1,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Recursive UDF

Hello

Is it possible to write a recursive (a function which calls itself) UDF
in DB2 (v.9) ?

When I load one, I get SQL0440N - function "XXX" not found...

Thanks in advance

--
Szymon Dembek
May 28 '07 #1
4 1871
Szymon Dembek wrote:
Hello

Is it possible to write a recursive (a function which calls itself) UDF
in DB2 (v.9) ?

When I load one, I get SQL0440N - function "XXX" not found...
You have to go through dynamic SQL. To do that in a UDF you need a
helper procedure.
Here is a recipe (untested):

--#SET TERMINATOR @
CREATE PROCEDURE poo(IN a INT, OUT aplus1 INT)
BEGIN
DECLARE txt VARCHAR(200);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR FOR stmt;
SET aplus1 = a;
IF a < 60 THEN
SET txt = 'SELECT FOO('|| CHAR(a) || ') + 1 FROM SYSIBM.SYSDUMMY1';
PREPARE stmt FROM txt;
OPEN cur;
FETCH cur INTO aplus1;
CLOSE cur1;
END IF;
END
@

CREATE FUNCTION FOO(a INT) RETURNS INT
BEGIN ATOMIC
DECLARE aplus1 INT;
CALL POO(a, aplus1);
RETURN aplus1;
END
@
--#SET TERMINATOR ;

VALUES FOO(1);

Note that the permitted level of recursion is 64 in DB2 9 (up from 16 in
DB2 V8)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 28 '07 #2
Serge Rielau wrote:
SET txt = 'SELECT FOO('|| CHAR(a) || ') + 1 FROM SYSIBM.SYSDUMMY1';
Note: In your production solution you want to use parameter markers for
the function arguments of course. I was a bit sloppy here.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 28 '07 #3
Serge Rielau wrote:
Serge Rielau wrote:
> SET txt = 'SELECT FOO('|| CHAR(a) || ') + 1 FROM SYSIBM.SYSDUMMY1';

Note: In your production solution you want to use parameter markers for
the function arguments of course. I was a bit sloppy here.
Thanks a lot !

--
Szymon Dembek
May 28 '07 #4
>Is it possible to write a recursive (a function which calls itself) UDF in DB2 (v.9) ? <<

Perhaps if you post your actual problem, we can find a way to avoid
recursion.

May 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
0
by: champ1979 | last post by:
I wrote an algorithm to get all the relatives of a person in a family tree. I'm basically getting all the users from the DB and am doing the recursive logic in code, so that there is only 1 call...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.