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 > Cursor support in functions

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-29-04, 08:46
deepaka deepaka is offline
Registered User
 
Join Date: Jan 2004
Posts: 20
Question Cursor support in functions

Hi All,

Do db2 user defined functions support cursors. I tried using a cursor in one of the functions and it gave me an error.

Thanks
Deepak
Reply With Quote
  #2 (permalink)  
Old 01-29-04, 11:21
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Deepak,
No, only in stored procedures.

Andy
Reply With Quote
  #3 (permalink)  
Old 01-29-04, 13:49
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Re: Cursor support in functions

You can use

FOR c1 AS
SELECT ......
DO
some processing
END FOR;

It acts as a CURSOR/LOOP combination.

Quote:
Originally posted by deepaka
Hi All,

Do db2 user defined functions support cursors. I tried using a cursor in one of the functions and it gave me an error.

Thanks
Deepak
Reply With Quote
  #4 (permalink)  
Old 01-30-04, 01:26
deepaka deepaka is offline
Registered User
 
Join Date: Jan 2004
Posts: 20
Thanks Guys,

I tried
FOR c1 AS
SELECT ......
DO
some processing
END FOR;

and it compiled. Will try to do processing and hopefully it will work too.
Reply With Quote
  #5 (permalink)  
Old 01-24-05, 13:30
allian allian is offline
Registered User
 
Join Date: Dec 2004
Posts: 54
can you please give example for this
Reply With Quote
  #6 (permalink)  
Old 01-24-05, 13:33
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Here is code that would be between the BEGIN..END for function body. It concatenates a list of codes together.

DECLARE v_sic_cde VARCHAR(40) DEFAULT ' ';

FOR sic AS
SELECT sic_cde
FROM sic_t
WHERE cust_id = p_cust_id <-- p_ passed into function
AND duns_sic_cde_i = 'Y'
AND row_exp_dtm IS NULL
ORDER BY sic_cde_seqid
DO
SET v_sic_cde = v_sic_cde || sic.sic_cde || ', ';

END FOR;

RETURN LTRIM(SUBSTR(v_sic_cde, 1, LENGTH(v_sic_cde) - 2));
Reply With Quote
  #7 (permalink)  
Old 01-24-05, 13:49
allian allian is offline
Registered User
 
Join Date: Dec 2004
Posts: 54
It is returning empty string

here is the code:

CREATE FUNCTION test_fn()

RETURNS varchar(500)
LANGUAGE SQL
READS SQL DATA
EXTERNAL ACTION
NOT DETERMINISTIC

BEGIN ATOMIC

declare final VARCHAR(500) default '';--
declare varia VARCHAR(10) default '';--

FOR varia AS SELECT user_id FROM cmd where cmd_id = 1 DO
SET final = final || varia || ', ';
END FOR;
RETURN rtrim(final) ;
END
@

Actually i need to return user_ids separed by comma as a single string.

Please can you help me
Reply With Quote
  #8 (permalink)  
Old 01-24-05, 13:54
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Just changed a couple of things. Try now.

CREATE FUNCTION test_fn()

RETURNS varchar(500)
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
NOT DETERMINISTIC

BEGIN ATOMIC

declare final VARCHAR(500) default '';

FOR varia AS SELECT user_id FROM cmd where cmd_id = 1 DO
SET final = final || varia.user_id || ', ';
END FOR;
RETURN LTRIM(SUBSTR(final, 1, LENGTH(final) - 2)); <-- strips the last ','
END
@
Reply With Quote
  #9 (permalink)  
Old 01-24-05, 14:27
allian allian is offline
Registered User
 
Join Date: Dec 2004
Posts: 54
Thanks a lot, its working fine.
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