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 > Want to return multiple integers from a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-18-11, 07:31
crozwise crozwise is offline
Registered User
 
Join Date: Apr 2011
Posts: 3
Unhappy Resolved - Want to return multiple integers from a table

Hi guys,

I am trying to return more than one values from a table. I have tried Return Table type, Cursor and none of these worked, i couldnt even run function i dont know why.

Here is my function works well if there is a 1 return;

CREATE FUNCTION "MAXIMO"."TEST" ()
RETURNS INTEGER
LANGUAGE SQL

BEGIN ATOMIC
DECLARE v_rowid INTEGER;
SET v_rowid = (select locid from locclas where location = 'GERMANY' and classificationid = 'INCIDENT')
RETURN v_rowid;
END;

When i want to set v_rowid, if there is more than 1 rows, it gives me an error.
I wonder how can i retrieve more than 1 values into v_rowid and return those values.

Thanks.

Last edited by crozwise; 04-19-11 at 06:30.
Reply With Quote
  #2 (permalink)  
Old 04-18-11, 08:05
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
It is better to remove double quotations.

CREATE FUNCTION MAXIMO.TEST ()
RETURNS TABLE (locid INTEGER)
LANGUAGE SQL
RETURN select locid from locclas where location = 'GERMANY' and classificationid = 'INCIDENT'
;
Reply With Quote
  #3 (permalink)  
Old 04-18-11, 08:24
crozwise crozwise is offline
Registered User
 
Join Date: Apr 2011
Posts: 3
Quote:
Originally Posted by tonkuma View Post
It is better to remove double quotations.

CREATE FUNCTION MAXIMO.TEST ()
RETURNS TABLE (locid INTEGER)
LANGUAGE SQL
RETURN select locid from locclas where location = 'GERMANY' and classificationid = 'INCIDENT'
;
I got the error again:

DB2 Database Error: ERROR [42887] [IBM][DB2/LINUXX8664] SQL0390N The function "MAXIMO.TEST3" resolved to specific function "SQL110418102057500" that is not valid in the context where it is used. SQLSTATE=42887

I am using Toad Freeware for DB2 4.0.1

Damn, i cant create Return Table type of a function

I dont get any error when i create a single return type function.
Reply With Quote
  #4 (permalink)  
Old 04-18-11, 08:43
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
First, I don't know Toad Freeware.
So, if the error was related to Toad environment, I can't help you.

When did you recieved the error SQL0390N?
If it was the time you executed it, how did you invoked the function?

What DB2 version(and platform OS) are you using?


Here is an example of table function tested on Command Editor.
Code:
------------------------------ Commands Entered ------------------------------
connect to SAMPLE ;
------------------------------------------------------------------------------

   Database Connection Information

 Database server        = DB2/NT 9.7.2
 SQL authorization ID   = DB2ADMIN
 Local database alias   = SAMPLE


A JDBC connection to the target has succeeded.
------------------------------ Commands Entered ------------------------------
CREATE FUNCTION test_emp()
RETURNS TABLE(empid INTEGER)
LANGUAGE SQL
RETURN SELECT INT(empno) FROM employee WHERE workdept = 'E11'
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT *
  FROM TABLE( test_emp() );
------------------------------------------------------------------------------

EMPID      
-----------
        310
        280
         90
        300
        290

  5 record(s) selected.
Reply With Quote
  #5 (permalink)  
Old 04-18-11, 08:57
crozwise crozwise is offline
Registered User
 
Join Date: Apr 2011
Posts: 3
That worked! I dont know how or why, but now i can execute create function queries similar to your example. Thanks a lot,

One more question,

Is this the simplest way to return multiple values?

Thanks
Reply With Quote
  #6 (permalink)  
Old 04-18-11, 09:03
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Quote:
Is this the simplest way to return multiple values?
I couldn't remember other ways than "RETURN SELECT ...." in a function.
Reply With Quote
  #7 (permalink)  
Old 04-18-11, 10:03
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Quote:
Quote:
Is this the simplest way to return multiple values?
I couldn't remember other ways than "RETURN SELECT ...." in a function.
Honesty, I ignored some other syntax.

RETURN VALUES ...
RETURN (SELECT ...
RETURN (VALUES ...

Anyway, I think that you can't use cursor in an SQL TABLE function.
Reply With Quote
  #8 (permalink)  
Old 04-19-11, 05:20
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The difference discussed here is between a scalar function, which can be used anywhere in SQL statements where expressions are allowed, and table functions, which return a table. Naturally, you cannot use the result of a table function as expression because comparing such a table T in a predicate like "T > 'abc'" has no semantics. Thus, you can query that table and work with the rows by calling the function in the WHERE clause as Tonkuma has shown.

Regarding the original question: if you always know how many INTs you want to return, you could also use a function that returns a single row with multiple (but a fixed number of) columns. However, that is still a table function since it does not return a single, scalar value.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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