473,769 Members | 2,134 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 3657
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(sub str(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
2921
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 key for all the keys. My code is shown below and the problem areas are marked with comments. In case you're wondering, this script is for generating sticky checkboxes that include an event handler. Many thanks to anyone who can help.
3
2080
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 constructed the following function: CREATE FUNCTION (@pUserId int)
1
4379
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 returndata. So 0 fits into a short so I have to convert to a short, although I say that my stored procedure has to return a NUMBER(16), thus int. Can anyone tell me how I can determine my return datatype beforehand?
4
10901
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 cursor is defined as: SQL_CURSOR delpt_cursor
2
5485
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, produce a "function not defined" message when the SELECT statement is executed, even having previously worked on that machine. What can I do to correct this difficulty when it occurs? What do I need to know about user-defined functions in SELECT...
3
4131
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 PERCENT dbo.PACZKI.ID_PACZKI, dbo.PACZKI.ID_AKCJI, dbo.PACZKI.NR_PACZKI, dbo.PACZKI.ILOSC_KUPONOW, LOG_1.Login AS LOGIN1,
13
1701
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 arguments, run it, and somehow get a return value. (Acutally, in the process of writing this post, I figured out a pretty good way of doing it. Here's the rest of my post and the solution I came up with. Enjoy my thought process!) Right now I'm...
17
3262
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
8
2220
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
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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 we have to send another system
2
3565
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.