Announcement Announcement Module
Collapse
No announcement yet.
How to pass parameters to a dynamic IN statement Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass parameters to a dynamic IN statement

    I have a query that have a statment
    Code:
    ... and SERVICE_CODE IN (?) ...
    I need to pass dynamic params to that IN statement, and i tried to pass an Array of Strings, or a List<String> or even a String that I manually treated by shaping them like " 'code1', 'code2', 'code3' ".
    It always fail and returns different kinds of SQL exceptions.
    Any idea?
    Note that I use external .SQL files.

  • #2
    You cannot... A ? represents a single value and that value is escaped and then added at that place (hence the failure for your manualy constructed list). A list isn't going to work as that is either added as is (basically calling toString on the list) or treated as multiple arguments which would fail due to missing placeholders in the sql.

    So basically you need to replace the single ? with multiple ? and just as much as there are in the list (beware that there is a limit for around 1000 placeholders in string).

    Another thing that could help is using the NamedParameterJdbcTemplate in which you can use named parameters instead of anonymous placeholders and that automatically takes care of the number of ? for your list.

    Comment


    • #3
      Thank you Marten,
      I will follow that lead and see what I can get by using NamedParameterJdbcTemplate.
      Thanks again for your help.

      Comment

      Working...
      X