473,505 Members | 14,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning user defined datatype from stored function.

How do I create and return user defined data types in DB2.

F.ex

In Oracle I can create a user define datatype and return this data
type from stored function. How can I do the same in DB2?

SELECT id, SYSDATE
FROM t
WHERE id IN ( SELECT * FROM THE ( SELECT CAST( fn( :1 ) AS mydatatype
) FROM dual ) );

The above statements helps me to use bind variables for IN clause SQL
statement. As the number of IN LIST items are dynamic, I can not
hard-code number of bind variables. So I use a database function which
returns me signle element (splits by comma internally). So I have only
one bind variable which gets value from my stored function.

How to do the same in DB2? How can I write DB2 compatible query?

Thanks

Sameer
Nov 12 '05 #1
4 3639
What you ask for here look like a user defined distinct type.
E.g.
CREATE DISTINCT TYPE commalist AS VARCHAR(2000) WITH COMPARISONS;
Teh most efficient woudl probably be to write a SQL Table function
which recursively parses teh list and spits out the elements.
An external table UDF (written e.g. in C) certainly would do the trick
as well.

The query would then look like:
SELECT id, CURRENT DATE
FROM t
WHERE id IN (SELECT id FROM TABLE(THE(CAST(? AS commalist)) AS THE)

The function;
CREATE FUNCTION THE(list COMMALIST) RETURNS TABLE(id INTEGER)
RETURN WITH rec(list, id, iter AS (VALUES (...., 1)
UNION ALL
SELECT ...., iter + 1 FROM rec
WHERE LENGTH(iter) > 0 AND iter < 2000)
SELECT id FROM rec;

Teh magc is in the .... ;-) A bunch of SUBSTR() POSSTR() and INTEGER()
should do it...

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Serge Rielau wrote:
What you ask for here look like a user defined distinct type.
E.g.
CREATE DISTINCT TYPE commalist AS VARCHAR(2000) WITH COMPARISONS;
Teh most efficient woudl probably be to write a SQL Table function
which recursively parses teh list and spits out the elements.
An external table UDF (written e.g. in C) certainly would do the trick
as well.

The query would then look like:
SELECT id, CURRENT DATE
FROM t
WHERE id IN (SELECT id FROM TABLE(THE(CAST(? AS commalist)) AS THE)

The function;
CREATE FUNCTION THE(list COMMALIST) RETURNS TABLE(id INTEGER)
RETURN WITH rec(list, id, iter AS (VALUES (...., 1)
UNION ALL
SELECT ...., iter + 1 FROM rec
WHERE LENGTH(iter) > 0 AND iter < 2000)
SELECT id FROM rec;

Teh magc is in the .... ;-) A bunch of SUBSTR() POSSTR() and INTEGER()
should do it...


You can have a look here to get the idea and an example:

http://www-106.ibm.com/developerwork...03stolze1.html
Back to the original question: have you considered using temporary tables?

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #3
Thanks Serge...

This is exactly what I am looking for..

In a while loop I can split the input string, but how do I append
individual item to table? I need to insert this in the dynamic table
which I want to return...

Is it possible to iterate in SQL statement whos results will be
returned as a table???

F. ex:

In Oracle, I do..

CREATE OR REPLACE TYPE myTableType AS TABLE OF NUMBER;

CREATE OR REPLACE FUNCTION fn_mystrtbl ( p_str in varchar2 ) RETURN
myTableType
AS
l_str LONG DEFAULT p_str || ',';
l_n NUMBER;
l_data myTableType := myTableType ();
BEGIN
LOOP
l_n := instr( l_str, ',' );
exit when (nvl(l_n,0) = 0);
l_data.extend;
l_data( l_data.count ) := ltrim(rtrim(substr(l_str,1,l_n-1)));
l_str := substr( l_str, l_n+1 );
END LOOP;
RETURN l_data;
END;
/

and the same old statement to return the data!! If you see the
function, I append each item to item to l_data() which is a table..

Thanks

Sameer
Nov 12 '05 #4
Thanks Knut!!
http://www-106.ibm.com/developerwork...03stolze1.html
This did my job!!!
Back to the original question: have you considered using temporary tables?


temp table?? I do not need temporary table here as I am not doing any
session specific thing.. May be I put the question in a different way
before... :-(

I use this for IN LIST SQL statements where usage of bind variables is
little tricky... I use this to make use of bind variables so my IN
clause SQL will not get parsed often!!

Thanks

Sameer
Nov 12 '05 #5

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

Similar topics

2
2905
by: Mountain Man | last post by:
Hi, I'm having trouble with the foreach function. I'm using it twice inside a user defined function with two different arrays, and in the second instance it's returning the value of the first...
3
2070
by: Marc Walgren | last post by:
Greetings I have an ASP application to enter reservations. There are multiple user security settings that require some users to have a restricted list of client in a drop list on a form. I...
1
4346
by: thepercival | last post by:
Hello, I have a stored procedure and the return data type is number(16) as you can see. but I get it back in the code as a var_numeric and then the precision depends on the value of the...
4
10877
by: Michael Trosen | last post by:
Hi Everyone, I hope someone can help, I'm pretty new to pro*c programming. I have the following application setup: a pro*c program calls a stored procedure and recieves a cursor back: the...
2
5473
by: David Emme | last post by:
Access 97 I have a number of SELECT statements which contain references to user-defined VBA functions. These typically work as expected, but occasionally, on one user's machine or another,...
3
4110
by: chreo | last post by:
I have user-defined function in MSSQL which returns Table (with 10 columns) (sorry for Polish names) CREATE FUNCTION PACZKI_Z_AKCJI (@AKCJA_ID int) RETURNS TABLE RETURN SELECT TOP 100...
13
1677
by: Kirk McDonald | last post by:
Say I have a database containing chunks of Python code. I already have a way to easily load, edit, and save them. What is slightly baffling to me is how I can effectively pass this code some...
17
3218
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
8
2189
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.