cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Array to IN operator within Database

rajendraj
New Contributor II

Scenario:
We are querying a table in Postgresql and retrieving records with ID.

The query output is similar to this:

[
{

    "record_id": [
        403,
        412,
        414
    ]
}

]

After completing the processing, I want to update the postgresql table for those record_id that the processing is complete.

Problem: Converted the record_id as:
โ€œ(โ€+record_id.map(x => parseInt(x)).join(โ€œ,โ€)+โ€œ)โ€ before sending this to the sql query (using JDBC execute)
After this step, my output is this: โ€œrecord_idSQLโ€: โ€œ(403,412,414)โ€
since record_idSQL is now a string, the moment I invoke the database query and using IN operator to find the rows with these record_ids, the adapter is adding a โ€™ โ€™ and it is erroring out.
From the error i get the following:

update abc.def set status = โ€˜3โ€™ where record_id in โ€˜(403,412,414)โ€™ was aborted. ERROR: syntax error at or near โ€œ$5โ€ Position.

record_id in postgresql is a int4 datatype and is sequence field with auto incrementing.

Any help is highly appreciated.

2 REPLIES 2

igormicev
Contributor

Hey @rajendraj

Why do you map(x => parseInt(x)) when you donโ€™t need to. Simply go with join(โ€œ,โ€). Next, when you obtain the output you shouldnโ€™t have something like โ€˜(403,412,414)โ€™ but just without the single quotes, i.e. (403,412,414).
Actually the single quotes are erroring out.

I made a test pipeline for updating in Postgres, which you can take a look:

PostgreSQL update example_2020_07_06.slp (4.8 KB)

BR, Igor Micev

rajendraj
New Contributor II

Igor,

Thanks for the solution. After I posted the question, I did solve by doing join but used ANY operator provided by Postgresql which made it much more simpler to solve in my case. Thanks once again though.