Link to home
Start Free TrialLog in
Avatar of petereilering
petereilering

asked on

SQL question

I want to do the following:

var list:string;
  begin

list:='("word1","word2"')'

query1.close;
query1.sql.clear;
query1.sql.add('select * from table1 where attribute IN list');

So the the In statement must use word1 en word2 to run the query.But the words are in the string variable.

Can somebody help me?
Avatar of petereilering
petereilering

ASKER

Edited text of question
I am using Delphi 2.0 and I have tried parambyname
ASKER CERTIFIED SOLUTION
Avatar of julio011597
julio011597

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi peterreilering,

SELECT *
FROM CLIENTS C
WHERE (C.ACCT_FirstName IN ('word1','word2'))

Does this work?

BTW, that is NOT a parametrized query... so the "feel free to ask more" :)
Hi all,

Julio are you sure your code works? If so, I've learned something more today....

Otherwise we've to create the string ourselfs :

var SQLString :String;

begin
 SQLString := 'where attribute IN ('
 SQLString := SQLString + ''''+ 'Word1'+ '''';
 SQLString := SQLString + ''''+ 'Word2'+ '''';
end;

Offcourse last would be better in a loop .....

for i := 0 to list.Count-1 do begin
 SQLString := SQLString + ''''+ List.Strings[i]+ '''';
end;

Don't forget to close it : SQLString := SQLString + ')';

And then :

query1.close;
    query1.sql.clear;
    query1.sql.add('Select *');
   query1.sql('from table1');
    query1.sql.add(SQLString);

Regards, ZiF.
Ok, this is a working method too, but if julio's code works, that one is better offcourse! :


var LISTATTRIBUTES : TStringList;
    i : integer;
    SQLSTRING : String;
begin
 LISTATTRIBUTES := TStringList.Create;
 with LISTATTRIBUTES do begin
  Clear;
  Add('Word1');
  Add('Word2');
 end;
 SQLString := '';
 SQLString := 'Where ATTRIBUTES IN (';
 for i:= 0 to LISTATTRIBUTES.COUNT-1 do begin
  SQLString := SQLString + '''' + LISTATTRIBUTES.Strings[i]+''''+',';
 end;
 SQLString[Length(SQLString)]:= ')';
 query1.sql.clear;
  query1.sql.add('Select *');
  query1.sql('from table1');
  query1.sql.add(SQLString);
 LISTATTRIBUTES.Free;
end;

Regards, ZiF.
Why shouldn't that work? I mean, Add() takes a String parameter, doesn't it?!

BTW, Zif, i wander if 'peter' is actually looking for something like your method - which is surely more general than mine - rather than a parametrized query...
Hi Julio, correct Add() takes a string, but I thought that he didn't meant list with just a string... I mean 500 points?.... I thought he meant with list a TStringList or something likt that... Regards, ZiF.
Right; indeed, IMO, this 500 points are just too much.
But where's petereilering? It seems to be his turn.
Julio excelent answer it works