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 > Select Query having IN keyword

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-26-11, 10:57
ashu000 ashu000 is offline
Registered User
 
Join Date: Apr 2011
Posts: 28
Red face Select Query having IN keyword

Hi All,

I have created so many queries for DB2 and successfully executed it.
I am facing problem in executing query which uses IN keyword.

I have a text box on page where user can enter any number of comma separated values. I need to query DB using this parameter as

SELECT * FROM STATE WHERE STATE_ABBREVIATION IN (:stateAbb);

Here, stateAbb value I will pass from my java program.
Lets say user has entered in text box two values - DC, AL.

In my java program I am taking these values one by one and making it as 'DC' and 'AL' and again appending these as 'DC', 'AL' because in query it must be in single quotes.
So, my query became (printed in console):
SELECT * FROM STATE WHERE STATE_ABBREVIATION IN ('DC', 'AL');


Now, when I send it and execute this, I get the following error :
nested exception is java.sql.SQLException: An undefined column name was detected

The same query when I run directly on sql page ..it runs successfully.

I posted the same problem in Java forums, in one reply I got that JDBC will not accept 'DC','AL' because you are sending it as a string.

I do not know why so?

If anybody have any idea or solution, please let me know.

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 06-27-11, 09:19
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Your problem is that you are trying to pass multiple items in one host variable. This cannot work. You are going to have to dynamically build the query, then execute it. You cannot use the host variable method to do this.

Andy
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