Skip to content
Advertisement

Groovy SQL named list parameter

I want to use a keyset of a Map as a list parameter in a SQL query:

I can use single values but no Sets or Lists. This is what I’ve tried so far:

The map uses Strings as key

Also, I don’t get an error. I just get nothing printed like have an empty result set.

Advertisement

Answer

You appoach will not give the expected result.

Logically you are using a predicate such as

This is the reason why you get no exception but also no data.

Quick search gives a little evidence, that a mapping of a collectiona to IN list is possible in Groovy SQL. Please check here and here

So very probably you’ll have to define the IN list in a proper length and assign the values from your array.

Anyway something like this works fine:

Data

Groovy Script

Result

The key step is to dynamicall prepare the IN list with proper names of teh bind variable using the experssion map.keySet().collect{":$it"}.join(',')

Note

You may also want to check the size if the map and handle the case where it is greater than 1000, which is an Oracle limitation of a single IN list.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement