Reputation: 343
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 ?
Upvotes: 1
Views: 1554
Reputation: 121932
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;
Upvotes: 2
Reputation: 6477
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.
Upvotes: 2