Question

I am using CheckListEdit component at Delphi which enables to select more than one item at time. When extracted using CheckListEdit1.Text, I get selected items in the String format like [item1, item2, item3] which I trimmed further to discard first and last character to get final string as 'item1, item2, item3'.

My code is as follows

glist := CheckListEdit1.Text;
glistindex := Length(glist);
Delete(glist,glistindex,1);
Delete(glist,1,1);

//Here I am getting glist = 'item1, item2, item3..' etc..

Now I want to select from data from table where group like item1, item2, item3 etc..

My SQL Query is as follows

UniTable1.SQL.Text := Select * from emp where group like (:grp);
UniTable1.Params.ParamByName('grp').Value := glist;

But above SQL is returning error as 'like item1, item2, item3' is not a proper format is there any alterante way to create SQL query so that it will return data from selected group at CheckListEdit ?

Was it helpful?

Solution

Instead of building a string as 'item1, item2, item3', you need to build it as '(item1, item2, item3)'. Then your query will become

Select * from emp where group in (item1, item2, item3)

As whosrdaddy pointed out, you will have to build the query as a concatenated string; it's not possible to pass the list of values as a parameter.

OTHER TIPS

Using parameter in the IN operator is not correct.You should either use an IN parameter list (:p1, :p2, ..., :pN) or use macros:

UniQuery.SQL.Text := 'Select * from emp where group in (&grp)';
UniQuery.MacroByName('grp').AsString := glist;
UniQuery.MacroByName('grp').Active := true;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top