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 > Function With In list parameter

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-27-11, 15:35
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Function With In list parameter

Hi i am trying to return a table from function my input parameter is list, i am not getting result set, query returns no data
Example

CREATE FUNCTION test (para char(100))
RETURNS TABLE (col1 char(7) )
NO EXTERNAL ACTION
F1: BEGIN ATOMIC
RETURN select col1 from admin.table1 where col2 in (para) ;
END
para = 100,101

result 0 records

expected result
Col1
A
B

if para = 100

result
Col1
A

if para = 101

result
Col1
B

Thanks

I am using DB2 v9.5
Reply With Quote
  #2 (permalink)  
Old 05-27-11, 16:24
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by phil72 View Post

expected result
Col1
A
B
Why would you expect that?


Quote:
Originally Posted by phil72 View Post
para = 100,101
Wrong. para = '100,101' (with 93 trailing blanks, by the way, because you used CHAR instead of VARCHAR). I hope you know what a character string is.

As a result, your query looks like this:
Code:
select col1 from admin.table1 where col2 in ('101,102               ')
Consider using dynamic SQL. Check EXECUTE IMMEDIATE in the manual.
Reply With Quote
  #3 (permalink)  
Old 05-27-11, 17:34
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
If no comma(',') in col2 was guaranteed,
try

CREATE FUNCTION test (para char(100))
RETURNS TABLE (col1 char(7) )
NO EXTERNAL ACTION
RETURN select col1 from admin.table1 where LOCATE(col2 , para) > 0
;

"BEGIN ... END" is not necessary.
Reply With Quote
  #4 (permalink)  
Old 05-27-11, 18:12
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by tonkuma View Post
select col1 from admin.table1 where LOCATE(col2 , para) > 0
Although this is logically correct, how likely is that that you'd use this in real life as opposed to a dynamically constructed statement? I'm sure you realize potential performance issues.
Reply With Quote
  #5 (permalink)  
Old 06-01-11, 10:20
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Thanks for the reply, the dynamic sql did not work. but locate is 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