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 > "WHERE IN" clause in a DB2 Cursor

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-04-09, 23:15
ek1975 ek1975 is offline
Registered User
 
Join Date: Sep 2003
Posts: 44
"WHERE IN" clause in a DB2 Cursor

I tried googling and couldn't find a good answer, so I am posting my question here. I want to use WHERE IN ('XXX', 'YYY', 'ZZZ'....) in a DB2 cursor. My problem is I don't know how many literals I would have to put in my WHERE IN clause. I may have anywhere from 1 to about 60.

I could either code a Dynamic SQL or I guess, code about 60 host variables and just use only the ones I require. Is there any other option other than the above to code a static SQL and still not code for 60 host variables...?

Environment : DB2 v8 on z/OS

Thanks.
Reply With Quote
  #2 (permalink)  
Old 04-05-09, 02:28
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by ek1975
I tried googling and couldn't find a good answer, so I am posting my question here. I want to use WHERE IN ('XXX', 'YYY', 'ZZZ'....) in a DB2 cursor. My problem is I don't know how many literals I would have to put in my WHERE IN clause. I may have anywhere from 1 to about 60.

I could either code a Dynamic SQL or I guess, code about 60 host variables and just use only the ones I require. Is there any other option other than the above to code a static SQL and still not code for 60 host variables...?

Environment : DB2 v8 on z/OS

Thanks.
Use a sub-select. If necessary the literals could be stored in a global temporary table.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 04-06-09, 22:26
ek1975 ek1975 is offline
Registered User
 
Join Date: Sep 2003
Posts: 44
Thanks Marcus, But I guess I am not getting it. I tried the subselect as follows:

<code>
SELECT COL2, COL3 FROM TABLE1 WHERE COL1 IN (
SELECT :WH-VAR1 FROM SYSIBM.SYSDUMMY1
)
</code>

followed by a "MOVE 'XXX, YYY' TO WH-VAR1" and it doesn't blow up, but gives me 'no rows found'. If I move just XXX or YYY, then it works. If you get a chance, I would really appreciate if you could point out my error please. Thanks for your time.
Reply With Quote
  #4 (permalink)  
Old 04-06-09, 23:20
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
How did you get the list of values ('XXX', 'YYY', 'ZZZ'....)?

Sometimes, you can write a single, straight-forward SQL statement instead of getting a list of values then pass the list to cursor.

Storze wrote rational opinion in this thread(for another process).
HOW TO PROCESS iN FOR LOOP?
Reply With Quote
  #5 (permalink)  
Old 04-07-09, 08:14
ek1975 ek1975 is offline
Registered User
 
Join Date: Sep 2003
Posts: 44
The values XXX, YYY or ZZZ come from a possible list of about 60 values selected by the end user on a front end screen (It's a HTML screen with a combo multiselect list box). So they may select one or they may select many. My app would need to find the matching records from the DB2 table
Reply With Quote
  #6 (permalink)  
Old 04-07-09, 09:50
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
So, you have about 60 fields.
Then I thought that IN (:var1, :var2, ..., var60) must be easy and natural.
It is not necessary to combine these 60 variables into one variable(:WH-VAR1).

Last edited by tonkuma; 04-07-09 at 09:55.
Reply With Quote
  #7 (permalink)  
Old 04-07-09, 13:32
ek1975 ek1975 is offline
Registered User
 
Join Date: Sep 2003
Posts: 44
Like I mentioned in my original post, I am aware of coding 60 host variables or coding a dynamic SQL. I wanted to know if there's a better way of doing it and I am trying to see if I can pin Marcus's suggestion (of using a subselect) down. Thanks.
Reply With Quote
  #8 (permalink)  
Old 04-08-09, 12:08
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
ek,
Marcus was suggesting something like:
select col from tablewhere somecol in (select somecol from my_temp_table)

and in your program you would have inserted 1 to 60 rows that contain the values you want to look for.

Dave
Reply With Quote
  #9 (permalink)  
Old 04-10-09, 13:25
ek1975 ek1975 is offline
Registered User
 
Join Date: Sep 2003
Posts: 44
Thanks Dave, I see it now.
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