Click to See Complete Forum and Search --> : SQL Query not working


snyder2005
04-22-2005, 10:55 AM
Can anyone see anything wrong with this statement ?

ResultSet res = statement.executeQuery(
"SELECT * "
+ "FROM CAPS_PARTICIPATION_ACTIVITY WHERE EMP_ID = '"
+ sEmp_id + "AND PRTPT_ACTV_ID =" + code_array[0] +
"AND ACTV_DT =" + date_range_array[0] + "'");

It returns 0 records, even though I know there are records matching this criteria. If I run the QUERY one criteria at a time, (say EMP_ID = sEmp_id, or ACTV_DT = date_range_array[0]), it finds the records, but not when I string them together in a compound statement. :confused:

Khalid Ali
04-22-2005, 03:02 PM
ResultSet res = statement.executeQuery(
"SELECT * "
+ "FROM CAPS_PARTICIPATION_ACTIVITY WHERE EMP_ID = '"
+ sEmp_id + "' AND PRTPT_ACTV_ID =" + code_array[0] +
"AND ACTV_DT ='" + date_range_array[0] + "'");

There are couple places where you have missed a single qoute, I have addedd them in red color. You have to make sure that your does conform to standard SQL.
Here is a tip for rest of your SQL life,

always write your query in note page replace java variables with some test values and run query directly on your db whichever it may be(if oracle then run it using sqlplus tool or sql worksheet tool).
Once the query is validate like that then just replace the value parts with your variables.

snyder2005
04-22-2005, 04:39 PM
Thanks a bunch ! I knew those stupid quotes were getting me. I'll look into using a tool like you suggested from now on. Thanks for the info. :cool: