dbTalk Databases Forums  

UDF returning three scalars

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss UDF returning three scalars in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Massimiliano Campagnoli
 
Posts: n/a

Default UDF returning three scalars - 06-15-2010 , 04:49 PM






Hello,
I need to develop an UDF which return three scalar values.
How can I group these three scalars together ?
I've tried

CREATE FUNCTION MYFUNC (P INT)
RETURNS ROW (I1 INT, I2 INT, I3 INT)
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
....

RETURN VALUES (v1, v2 , v3)

but how can I invoke it and use it in a select list ?

SELECT MYFUNC(1) from ...

does not work.

Can you please explain me how can I invoke this function ?

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: UDF returning three scalars - 06-15-2010 , 05:07 PM






On 2010-06-15 22:49, Massimiliano Campagnoli wrote:
Quote:
Hello,
I need to develop an UDF which return three scalar values.
How can I group these three scalars together ?
I've tried

CREATE FUNCTION MYFUNC (P INT)
RETURNS ROW (I1 INT, I2 INT, I3 INT)
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
....

RETURN VALUES (v1, v2 , v3)

but how can I invoke it and use it in a select list ?

SELECT MYFUNC(1) from ...

does not work.

Can you please explain me how can I invoke this function ?
First (you don't mention neither platform nor version, so I'll assume
LUW 9.5):

http://publib.boulder.ibm.com/infoce.../r0004240.html

ROW column-list
Specifies that the output of the function is a single row. If the
function returns more than one row, an error is raised (SQLSTATE 21505).
The column-list must include at least two columns (SQLSTATE 428F0).

A row function can only be used as a transform function for a
structured type (having one structured type as its parameter and
returning only base types).

So perhaps it is a table function you need? You create one like:

CREATE FUNCTION MYFUNC (P INT)
RETURNS TABLE (I1 INT, I2 INT, I3 INT)
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
.....
RETURN VALUES (v1, v2, v3)

and you invoke it with:

select * from table(myfunc(...))

Reply With Quote
  #3  
Old   
Massimiliano Campagnoli
 
Posts: n/a

Default Re: UDF returning three scalars - 06-16-2010 , 02:43 PM



On 15 Giu, 23:07, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:
Quote:
On 2010-06-15 22:49, Massimiliano Campagnoli wrote:





Hello,
I need to develop an UDF which return three scalar values.
How can I group these three scalars together ?
I've tried

*CREATE FUNCTION MYFUNC (P INT)
* * *RETURNS ROW (I1 INT, I2 INT, I3 INT)
* * *LANGUAGE SQL
* * *CONTAINS SQL
* * *NO EXTERNAL ACTION
* * *DETERMINISTIC
* * * ....

* * *RETURN VALUES (v1, v2 , v3)

but how can I invoke it and use it in a select list ?

SELECT MYFUNC(1) from ...

does not work.

Can you please explain me how can I invoke this function ?

First (you don't mention neither platform nor version, so I'll assume
LUW 9.5):

http://publib.boulder.ibm.com/infoce...dex.jsp?topic=....

ROW column-list
* * Specifies that the output of the function is a single row. If the
function returns more than one row, an error is raised (SQLSTATE 21505).
The column-list must include at least two columns (SQLSTATE 428F0).

* * A row function can only be used as a transform function for a
structured type (having one structured type as its parameter and
returning only base types).

So perhaps it is a table function you need? You create one like:

CREATE FUNCTION MYFUNC (P INT)
RETURNS TABLE (I1 INT, I2 INT, I3 INT)
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
....
RETURN VALUES (v1, v2, v3)

and you invoke it with:

select * from table(myfunc(...))
thanks,
so you invoke in the same way, using same syntax, a function returning
a single row and a function returning a full table.

Reply With Quote
  #4  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: UDF returning three scalars - 06-16-2010 , 03:25 PM



On 2010-06-16 20:43, Massimiliano Campagnoli wrote:
Quote:
On 15 Giu, 23:07, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com
wrote:
On 2010-06-15 22:49, Massimiliano Campagnoli wrote:





Hello,
I need to develop an UDF which return three scalar values.
How can I group these three scalars together ?
I've tried

CREATE FUNCTION MYFUNC (P INT)
RETURNS ROW (I1 INT, I2 INT, I3 INT)
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
....

RETURN VALUES (v1, v2 , v3)

but how can I invoke it and use it in a select list ?

SELECT MYFUNC(1) from ...

does not work.

Can you please explain me how can I invoke this function ?

First (you don't mention neither platform nor version, so I'll assume
LUW 9.5):

http://publib.boulder.ibm.com/infoce...dex.jsp?topic=...

ROW column-list
Specifies that the output of the function is a single row. If the
function returns more than one row, an error is raised (SQLSTATE 21505).
The column-list must include at least two columns (SQLSTATE 428F0).

A row function can only be used as a transform function for a
structured type (having one structured type as its parameter and
returning only base types).

So perhaps it is a table function you need? You create one like:

CREATE FUNCTION MYFUNC (P INT)
RETURNS TABLE (I1 INT, I2 INT, I3 INT)
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
....
RETURN VALUES (v1, v2, v3)

and you invoke it with:

select * from table(myfunc(...))

thanks,
so you invoke in the same way, using same syntax, a function returning
a single row and a function returning a full table.
Yes, a table with one row. Example:

[...]$ db2 -v -td@ -f f.sql
drop function myfun
DB20000I The SQL command completed successfully.

create function myfun (p int)
returns table( i1 int, i2 int, i3 int )
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC

return values (p, p*p, p*p*p)


DB20000I The SQL command completed successfully.

[...]$ db2 "select * from table(myfun(3))"

I1 I2 I3
----------- ----------- -----------
3 9 27

1 record(s) selected.

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.