I have a MS Query
connection from Excel 2016
sheet to IBM DB2
database. I use parameterized query and link parameter values to Excel cell. While "singular value" clauses such as = < > like
work, I can't get IN
clause to work with multiple values but otherwise super simple query.
Here's a simple demo dataset how I produce the parameter values:
Column D formula is =IF(C2>5,A2&",","")
which checks column C value for higher than 5 and populates ID
in column D if TRUE
. I'm expecting to use a helper cell to merge/concat all values in column D which I want to use for IN
clause as value (yet to be completed).
How can I use "value1, value2, value3, ..."
in a cell to run an IN
clause query? Is it possible at all? IN
clause works fine when the linked cell holds value 1
however 1, 3
produces error message:
"Bad parameter type. Microsoft Excel is expecting a different kind of value than what was provided"
.
Using a separate parameter and OR
clause for each cell technically looks to work, however in practice the dataset can easily reach 50-100 values which means crazy amount of parameter setup.
Query itself is something as simple as:
select * from PRODUCTS a
where a.prod_ID in (1,3)
or as Excel MS Query alternative:
select * from PRODUCTS a
where a.prod_ID in ?
posting this here for future googlers. So the solution was a bit out of the box for me - although still needs enhancement.
It checks for worksheet change events in range M1:M4 and then populates IN-clause query command text according to the prepared syntax in B1. Really out of the box for me!
Unfortunately it only handles a single Listobject (1) here, although I have more than one which should get the same treatment - but nevertheless, great stuff.
This:
Will not ever work.
What would work, is passing the string into a function that can return a set of rows.
split()
is a user defined table function, taken from IBM's developer works article: How to split a string into a set of rows (anti-LISTAGG)Depending on your platform and version of Db2, there may be some built in options.
On recent versions for instance, you could pass in an XML or JSON string with the values and use XMLTABLE() or JSON_TABLE() to split the string into rows.