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 > Problems in creating a DB2 UDF .. Please help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-10, 08:44
r2k1984 r2k1984 is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
Problems in creating a DB2 UDF .. Please help

Hai,

I was trying to create a user defined function in DB2. I was not able to create the function and was getting few errors. Please find the code and the error message.



CREATE FUNCTION DESC_FUNC(v_rpt_id integer, v_desc_typ_cde varchar(1))
RETURNS VARCHAR(32672)
DECLARE v_desc VARCHAR(32672) default '';
for1: BEGIN ATOMIC
FOR tmp_row AS
{
SELECT
desc_seq_num, evnt_desc_txt
FROM
"t_flt_sfty_desc DES","t_flt_sfty_rpt rptin1"
WHERE DES.EVNT_DTE=rptin1.EVNT_DTE
AND DES.EVNT_SEQ_NUM=rptin1.EVNT_SEQ_NUM
AND DES.RPT_EVNT_ORD_NUM = rptin1.RPT_EVNT_ORD_NUM
AND DES.RPTEE_TYP_CDE = rptin1.RPTEE_TYP_CDE
AND DES.View_typ_cde = 'D'
AND desc_typ_cde = v_desc_typ_cde
AND rpt_id= v_rpt_id
ORDER BY desc_seq_num
)
}
DO
IF desc_seq_num =1 THEN
SET v_desc = tmp_row.evnt_desc_txt;
ELSE
SET v_desc = v_desc ||tmp_row.evnt_desc_txt;
END IF;
END FOR for1;
return v_desc;
END


I got the following error message

DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "V_DESC" was found following "". Expected
tokens may include: "ON AFTER <INTEGER>". SQLSTATE=42601



Can any one please solve this.
Reply With Quote
  #2 (permalink)  
Old 04-06-10, 08:47
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Declare your variable inside the BEGIN block, not outside.

Andy
Reply With Quote
  #3 (permalink)  
Old 04-06-10, 09:07
r2k1984 r2k1984 is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
Thanks Andy.

I tried executing the query like below.

CREATE FUNCTION DESC_FUNC(v_rpt_id integer, v_desc_typ_cde varchar(1))
RETURNS VARCHAR(32672)

for1: BEGIN ATOMIC
DECLARE v_desc VARCHAR(32672) default '';
FOR tmp_row AS
{
SELECT
desc_seq_num,varchar(evnt_desc_txt) evnt_desc_txt
FROM
db2fsr.t_flt_sfty_desc DES,db2fsr.t_flt_sfty_rpt rptin1
WHERE DES.EVNT_DTE=rptin1.EVNT_DTE
AND DES.EVNT_SEQ_NUM=rptin1.EVNT_SEQ_NUM
AND DES.RPT_EVNT_ORD_NUM = rptin1.RPT_EVNT_ORD_NUM
AND DES.RPTEE_TYP_CDE = rptin1.RPTEE_TYP_CDE
AND DES.View_typ_cde = 'D'
AND desc_typ_cde = 'D'
AND rpt_id= 11136
ORDER BY
desc_seq_num
)
}
DO
IF desc_seq_num =1 THEN
SET v_desc = tmp_row.evnt_desc_txt;
ELSE
SET v_desc = v_desc ||tmp_row.evnt_desc_txt;
END IF;
END FOR for1;
return v_desc;
END


Iam getting the following error again

DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token ":" was found following "". Expected tokens may
include: "ON AFTER <INTEGER>". SQLSTATE=42601

Last edited by r2k1984; 04-06-10 at 09:10.
Reply With Quote
  #4 (permalink)  
Old 04-06-10, 09:20
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Your syntax is wrong. Use the manuals for the correct syntax. What are the squiggle brackets for?

Andy
Reply With Quote
  #5 (permalink)  
Old 04-06-10, 10:22
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Yet another approach is to search for "string concatenation". Because what you try to do is a simple recursive query or you could use the XMLAGG function.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 04-06-10, 10:30
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,194
The function body may be written by a RETURN statement, like this...
(DB2 for LUW 9.5 or later)
Code:
CREATE FUNCTION DESC_FUNC(v_rpt_id integer, v_desc_typ_cde varchar(1) ) 
RETURNS VARCHAR(32672) 
RETURN
SELECT XMLCAST(
         XMLGROUP( evnt_desc_txt
                   ORDER BY desc_seq_num
                 )
         AS VARCHAR(32672)
       )
  FROM t_flt_sfty_desc DES
     , t_flt_sfty_rpt  rptin1 
 WHERE DES.EVNT_DTE         = rptin1.EVNT_DTE 
   AND DES.EVNT_SEQ_NUM     = rptin1.EVNT_SEQ_NUM 
   AND DES.RPT_EVNT_ORD_NUM = rptin1.RPT_EVNT_ORD_NUM 
   AND DES.RPTEE_TYP_CDE    = rptin1.RPTEE_TYP_CDE 
   AND DES.View_typ_cde     = 'D' 
   AND desc_typ_cde         = v_desc_typ_cde 
   AND rpt_id               = v_rpt_id 
;
If you are using DB2 for LUW 9.1 or earlier,
you can use XMLSERIALIZE and XMLAGG instead of XMLCAST and XMLGROUP.
Reply With Quote
  #7 (permalink)  
Old 04-07-10, 01:26
r2k1984 r2k1984 is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
Thanks Guys for the help. But nothing above worked for me.

I tried executing my first query without ). Iam getting the same error.

Iam using db2 8 so XMLCAst will not work ..I tried executing the above query but i was getting the below error.

B21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returnedQL0104N An unexpected token "(" was found following "". Expected tokens may include: "ON AFTER <INTEGER>". SQLSTATE=42601

SQL0104N An unexpected token "(" was found following "". Expected tokens may include: "ON AFTER <INTEGER>
Reply With Quote
  #8 (permalink)  
Old 04-07-10, 02:40
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You'll find some working examples here: How to concatenate rows together? | db2ude (I searched for "DB2 string concatenation recursion")
__________________
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