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

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
tommy
Posts: 11
Joined: Thu 21 Jul 2016 21:52

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

Post by tommy » Tue 26 Jul 2016 20:47

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.

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

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

Post by MaximG » Thu 28 Jul 2016 11:37

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';

Post Reply