To my Oracle DB I want to send via JDBC and a prepared statement SQL query like this:
SELECT * FROM mytable WHERE col1 = <ONEVAL> col2 = <ONEVAL> AND col3 = <ONEVAL>;
I know from SO answer How to use the same value multiple times in jdbc postgresql prepared statement that JDBC doesn't support named parameters, so a direct prepared statement
SELECT * FROM mytable WHERE col1 = ? AND col2 = ? AND col3 = ?;
only works if I provide <ONEVAL>
3 times, but I do not want to do that, I rather would like to adjust the SQL of the prepared statement, so that it takes only one ?
but applies it to all WHERE
conditions. Something like
WITH VAl = ? SELECT * FROM mytable WHERE col1 = VAL AND col2 = VAL AND col3 = VAL;
But this try doesn't seem to work. Is there way to do this kind of multi-use of one prepared statement parameter in the SQL string?
After the kind suggestion by @Selvin I used the following SQL string for the prepared statement:
DECLARE val varchar(30) :=? ; SELECT * FROM mytable WHERE col1=&val AND col2=&val AND col3=&val
Then I get the error "ORA-06550: line 1, column 33: PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: [...]"
If I got it right, you need all three columns to be equal to the same value at the same time, so my guess is that these SQL will help you
SELECT * FROM mytable WHERE col1 = ? AND col1 = col2 AND col1 = col3;
UPD: There is a better or I'd say more compact idea
SELECT * FROM mytable WHERE ? = all(col1, col2, col3)