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 > Help with Db2 User Defined Function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-01-11, 23:17
abhegade abhegade is offline
Registered User
 
Join Date: Aug 2011
Posts: 2
Help with Db2 User Defined Function

Hi,

I need help with a user defined function.
I am getting an error with the below function

CREATE OR REPLACE FUNCTION GETDSSRANGE (AMOUNT DECIMAL(18,2))
RETURNS SMALLINT
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC
DECLARE RNG_CD SMALLINT;
SELECT CAST(DSS_RNG_CD AS SMALLINT) FROM INTDM.TBINTL_RNG_CD_XREF WHERE GETDSSRANGE.AMOUNT BETWEEN DSS_LWLM_NO AND DSS_UPLM_NO AND DSS_RNG_TY_NM ='ASSET' ;
SET RNG_CD=DSS_RNG_CD ;
RETURN RNG_CD;
END

Error I am getting :
SQL0104N An unexpected token "smallint" was found following "TOMIC declare rng_cd". Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=1. SQLSTATE=42601
Reply With Quote
  #2 (permalink)  
Old 08-01-11, 23:55
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
1) Did you specified statement termination character?

2) Even if you could create the function with no error,
I guessed that you can't return the result from the select statement.
So, please try...
Code:
CREATE OR REPLACE FUNCTION
 GETDSSRANGE (AMOUNT DECIMAL(18,2))
RETURNS SMALLINT 
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN
SELECT CAST(DSS_RNG_CD AS SMALLINT)
 FROM  INTDM.TBINTL_RNG_CD_XREF
 WHERE GETDSSRANGE.AMOUNT BETWEEN DSS_LWLM_NO AND DSS_UPLM_NO
   AND DSS_RNG_TY_NM ='ASSET'
;
Reply With Quote
  #3 (permalink)  
Old 08-01-11, 23:57
abhegade abhegade is offline
Registered User
 
Join Date: Aug 2011
Posts: 2
I need to return a scalar. Above code will return a table. And i am a bit confused how to use the statement termination.
My code has semicolons. So should i be using db2 -td -f code.sql o execute it?

Last edited by abhegade; 08-02-11 at 00:11.
Reply With Quote
  #4 (permalink)  
Old 08-02-11, 00:24
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Above code will return a table
No.
If the select statement in your code returns only one row,
the return statement in my example will return a scalar value.

If the select statement in your code returns multiple rows,
you should decide a row which should be returned.
One way may be to add FETCH FIRST 1 ROW ONLY (and optional ORDER BY clause), like...
Code:
CREATE OR REPLACE FUNCTION
...
...
RETURN
SELECT CAST(DSS_RNG_CD AS SMALLINT)
 FROM  INTDM.TBINTL_RNG_CD_XREF
 WHERE GETDSSRANGE.AMOUNT BETWEEN DSS_LWLM_NO AND DSS_UPLM_NO
   AND DSS_RNG_TY_NM ='ASSET'
 ORDER BY ...
 FETCH FIRST 1 ROW ONLY
;
Reply With Quote
  #5 (permalink)  
Old 08-02-11, 00:28
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Here is an example of SQL scalar function having only a return statement in the body.

Code:
------------------------------ Commands Entered ------------------------------
CREATE OR REPLACE FUNCTION
 test_get_wordept( in_edlevel INT )
RETURNS CHAR(3)
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN
SELECT workdept
 FROM  employee
 GROUP BY
       workdept 
 HAVING
       in_edlevel
       BETWEEN MIN(edlevel) AND MAX(edlevel)
 ORDER BY
       workdept
 FETCH FIRST 1 ROW ONLY
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.
Code:
------------------------------ Commands Entered ------------------------------
VALUES test_get_wordept(15);
------------------------------------------------------------------------------

1  
---
A00

  1 record(s) selected.
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