First of all you should know that there is limit for the data can be in the IN
clause. So if you have a large number of comma separated values to pass as the parameter (bind variable) of the IN
clause you should manage something else.
Now if you have a limited number of ids (for example less than 500 or so, depends on the size of ids which totally can not be more than 4000 characters) you can use the regexp_substr
along with connect by
to tokenize the comma separated values of your single parameter ?
in the where clause: to_char(id) in (?)
:
SELECT REGEXP_SUBSTR(? ,'[^,]+', 1, LEVEL) FROM dual
CONNECT BY REGEXP_SUBSTR(? , '[^,]+', 1, LEVEL) IS NOT NULL
when you pass the string "'12','34', '444'"
as the parameter, it would tokenize them as rows:
'12'
'34'
'444'
So theoretically you can change your query in this way:
SELECT *
FROM table1
WHERE to_char(id) in
(
SELECT REGEXP_SUBSTR(? ,'[^,]+', 1, LEVEL) FROM dual
CONNECT BY REGEXP_SUBSTR(? , '[^,]+', 1, LEVEL) IS NOT NULL
)
But in this way if the size of the table1
is big the performance would be so bad because of the conversion of the id
for every record.
It's better to convert your limited number of parameters of the IN
clause to enhance the performance:
SELECT *
FROM table1
WHERE id in
(
SELECT REGEXP_SUBSTR(? ,'[^,]+', 1, LEVEL) FROM dual
CONNECT BY REGEXP_SUBSTR(? , '[^,]+', 1, LEVEL) IS NOT NULL
)
and in your java code you should pass the string "12,34, 444"
as the parameter (without single single quotes around each id).
You should know that this solution is not optimized for a large number of ids. However, it makes the number of parameters of the IN
clause dynamic.
Hope this helps.