🎉Community Raffle - Win $25

An exclusive raffle opportunity for active members like you! Complete your profile, answer questions and get your first accepted badge to enter the raffle.
Join and Win

Read Database, query sql in

User: "WentingLi13"
New Altair Community Member
Updated by Jocelyn
My query sql is SELECT xxxxxx FROM table.name WHERE columnA in (?)
I want to define a macro which name %{list} to instead of ?
I've tried %{list} value is A, execute the query success, but when value is A,B, my query result is null, so i want to know, which separate charactor should i use when there is multi value in SQL IN(?)

Find more posts tagged with

Sort by:
1 - 2 of 21
    User: "rfuentealba"
    New Altair Community Member
    Accepted Answer
    Updated by rfuentealba
    Ahhh you are using parameters.

    It won't work, because parameters are meant to sanitize the SQL query you are creating. Just use this as your SQL query:
    SELECT FRLOT.LOT_ID, FRLOT.LOT TYPE FROM FRLOT WHERE FRLOT.LOT_ID IN (%{lot})
    Notice that it is a bad practice to pass macros as non parameters, but for now and until more help is coming, I believe that this will work.

    @BalazsBarany might have a better idea on how to solve this, maybe?

    All the best,

    Rod.
    User: "BalazsBaranyRM"
    New Altair Community Member
    Accepted Answer
    Hi!

    I wouldn't says it's a very bad practice to pass macro values in every case, but it can lead to SQL injection attacks. If you control the inputs of your process, and use a read-only access to you database, not a lot of bad stuff can happen.

    If you have a dynamic list of parameters with a specified format, and you'd like to use the prepared query with ? as the parameter placeholder, you can split up these yourself. The functions for this are unfortunately database dependent.

    In Postgres you would do it like this:

    with inputs as (
    	select regexp_split_to_table('1, 2, 3, 4', ', *') as param
    )
    select param
    from inputs
    Here you can use WHERE value IN (SELECT param FROM inputs), and you could pass the '1, 2, 3, 4' value as a ? parameter. This setup makes sure that the format and data types are correct, and avoid the danger of SQL injection.

    Regards,
    Balázs