Page 1 of 1

Howto use parameters in SELECT WHERE ... IN ... query

Posted: Tue 26 Jul 2016 20:47
by tommy
Hello,

I want to create a statement like 'SELECT * FROM table WHERE parameter IN (valuelist);'

I know how to use parameters in general, e.g.
q.SQL.Text := 'SELECT * FROM table WHERE id=:ID;'
q.ParamByName('id').AsInteger := 42;

But for the IN-statement, I have to pass an array and do not know how to do it. There is no ParamByName().AsArray property.

Re: Howto use parameters in SELECT WHERE ... IN ... query

Posted: Thu 28 Jul 2016 11:37
by MaximG
Please specify the database you are working with. Usage of the AsArray property requires support at the DBMS side. In case of a fixed number of elements of the parameter list, you can describe each of them in a query:

Code: Select all

'SELECT * FROM table WHERE parameter IN (:value1, :value2, :value3, :value4)' 
In case of varying number of elements, you can use a macro:

Code: Select all

'SELECT * FROM table WHERE parameter IN (&valuelist)' 
In this case, list values can be filled in as follows:

Code: Select all

q.MacroByName('valuelist').Value := '7521, 7788, 7900';