Results 1 to 3 of 3

Thread: How to pass parameters to a dynamic IN statement

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2012
    Posts
    2

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •