Solved

SQL question

Posted on 1998-05-04
11
142 Views
Last Modified: 2010-04-06
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?
0
Comment
Question by:petereilering
  • 4
  • 4
  • 3
11 Comments
 
LVL 1

Author Comment

by:petereilering
ID: 1341186
Edited text of question
0
 
LVL 1

Author Comment

by:petereilering
ID: 1341187
I am using Delphi 2.0 and I have tried parambyname
0
 
LVL 5

Accepted Solution

by:
julio011597 earned 500 total points
ID: 1341188
Have you tried:

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

Feel free to ask more.

-julio
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 8

Expert Comment

by:ZifNab
ID: 1341189
Hi peterreilering,

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

Does this work?

0
 
LVL 5

Expert Comment

by:julio011597
ID: 1341190
BTW, that is NOT a parametrized query... so the "feel free to ask more" :)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341191
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.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341192
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.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1341193
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...
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341194
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.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1341195
Right; indeed, IMO, this 500 points are just too much.
But where's petereilering? It seems to be his turn.
0
 
LVL 1

Author Comment

by:petereilering
ID: 1341196
Julio excelent answer it works
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question