Solved

Delphi SQL 'And/Or' Problem

Posted on 2009-05-19
7
199 Views
Last Modified: 2012-05-07
I want the code to return 18-year-olds who scored grade A,B or C. Instead it gives me all ages who scored A, B, Or C. I'm confused by the use of 'Where/And/Or'  in an SQL statement, particularly applied to Delphi code. Help much appreciated.
Qresults.Active:=True;
Qresults.SQL.Clear;
Qresults.SQL.Add('SELECT * FROM ResultsTable');
Qresults.SQL.Add('WHERE age:=+IntToStr(18));
Qresults.SQL.Add('AND  Grade='+QuotedStr('A'));
Qresults.SQL.Add('OR Grade='+QuotedStr('B'));
Qresults.SQL.Add('OR Grade='+QuotedStr('C'));
Qresults.Open;

Open in new window

0
Comment
Question by:nickjfox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Expert Comment

by:FactorB
ID: 24423806
Looks like you need some brackets, try it this way:

Qresults.Active:=True;
Qresults.SQL.Clear;
Qresults.SQL.Add('SELECT * FROM ResultsTable');
Qresults.SQL.Add(' WHERE (age:='+IntToStr(18));
Qresults.SQL.Add(' AND  (Grade='+QuotedStr('A'));
Qresults.SQL.Add(' OR Grade='+QuotedStr('B'));
Qresults.SQL.Add(' OR Grade='+QuotedStr('C')+'))');
Qresults.Open;

Regards,
B.
0
 
LVL 6

Accepted Solution

by:
FactorB earned 175 total points
ID: 24423832
Ups also after age there is ":=" instead "=" looks like Delphy/SQL hybrid, remove ":"

Qresults.Active:=True;
Qresults.SQL.Clear;
Qresults.SQL.Add('SELECT * FROM ResultsTable');
Qresults.SQL.Add(' WHERE (age='+IntToStr(18));
Qresults.SQL.Add(' AND  (Grade='+QuotedStr('A'));
Qresults.SQL.Add(' OR Grade='+QuotedStr('B'));
Qresults.SQL.Add(' OR Grade='+QuotedStr('C')+'))');
Qresults.Open;
0
 
LVL 22

Assisted Solution

by:8080_Diver
8080_Diver earned 75 total points
ID: 24424001
Essentially, you can follow the concept of using parentheses that you should have learned in your math classes.  Parentheses are a tool for clarification of the intent of the calculation (in this case, a boolean calculation).  So, any time when you either have concerns about the order in which things will happen or you want to force the order in which things happen, you can arrange the parentheses so that you can accomplish your goal.
On the other hand, the particular situation you are dealing with lends itself to the use of the IN clause, as is shown in the Delphi 2: code below.
However, one point to notice in the provided statements that will probably cause issues is the following line (as compared to the code I provide below):
Qresults.Active:=True;
 
That line should be:
Qresults.Active:=False;
 
There will also be problems from the lack of a space character between some of the elements in the SQL statement that is built with the other code offerings.  And, I would advise listing the columns that you are selecting rather than relying on the asterisk.  (That comes from the SQL Developer in me . . . using the SELECT * form is laible to cause you to retrieve a lot of unnecessary data from the columns that are in the table but not being used from the query. ;-)
 

Delphi 1:
 
if Qresults.Active then Qresults.Active:=False;
Qresults.SQL.Clear;
Qresults.SQL.Add('SELECT * ');
Qresults.SQL.Add('FROM   ResultsTable ');
Qresults.SQL.Add('WHERE  (age = ' + IntToStr(18));
Qresults.SQL.Add('  AND  ((Grade = ' + QuotedStr('A') + ' OR ');
Qresults.SQL.Add('        (Grade = ' + QuotedStr('B') + ' OR ');
Qresults.SQL.Add('        (Grade = ' + QuotedStr('C') + '))');
Qresults.Open;
 
Delphi 1:
 
if Qresults.Active then Qresults.Active:=False;
Qresults.SQL.Clear;
Qresults.SQL.Add('SELECT * ');
Qresults.SQL.Add('FROM   ResultsTable ');
Qresults.SQL.Add('WHERE  (age = ' + IntToStr(18));
Qresults.SQL.Add('  AND  (Grade IN (' + QuotedStr('A') + ', ');
Qresults.SQL.Add('                  QuotedStr('B') + ', ');
Qresults.SQL.Add('                  QuotedStr('C') + '))');
Qresults.Open;

Open in new window

0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:nickjfox
ID: 24424542
OK, I gave a simplified version of my problem in the hope that once I grasped the concept I'd be able to do the rest myself. So, thanks, to both of you, I'm almost there. But I still seem a bracket short? Or one bracket too many? I count five extra brackets but it won't work.
QResullts.SQL.Clear;
QResullts.SQL.Add('SELECT * FROM ResultsTable');
QResullts.SQL.Add('WHERE (age<='+IntToStr(18));
QResullts.SQL.Add('AND (passed>'+IntToStr(0));
QResullts.SQL.Add('AND (pass_Type<>null');
QResullts.SQL.Add('AND (Overwork<>null');
QResullts.SQL.Add(' AND (Grade='+QuotedStr('A'));
QResullts.SQL.Add(' OR Grade='+QuotedStr('G'));
QResullts.SQL.Add(' OR  Grade='+QuotedStr('I'));
QResullts.SQL.Add(' OR Grade='+QuotedStr('HA'));
QResullts.SQL.Add(' OR  Grade='+QuotedStr('L'));
QResullts.SQL.Add(' OR Grade='+QuotedStr('P'));
QResullts.SQL.Add(' OR Grade='+QuotedStr('Q'));
QResullts.SQL.Add(' OR Grade='+QuotedStr('R'));
QResullts.SQL.Add(' OR Grade='+QuotedStr('T'));
QResullts.SQL.Add(' OR Grade='+QuotedStr('X')+')))))');
QResullts.Open;

Open in new window

0
 

Author Comment

by:nickjfox
ID: 24424631
No, my mistake, the code as last posted works fine. It was something above causing a problem.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24425512
you will produce a lot of problems on the database this way.
use bind variables, or parameters !

next to that consider using ALT+mouse drag to select the sql text within the quotes:
(only works in advanced editors like in Delphi)
QResults.SQL.Text := 
  'SELECT * FROM ResultsTable    '+
  'WHERE (age<= :minage)         '+
  '  AND (passed > :minpass)     '+
  '  AND (pass_Type is not null) '+
  '  AND (Overwork is not null)  ';
  // rest of lines
QResults.Prepare;
QResults.ParamByName('minage').AsInteger := 18;
QResults.ParamByName('minpass').AsInteger := 0;
QResults.Open;

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24425755
@Geert_Gruwez,
Actually, if this is done on the fly and prepared every time, it isn't going to matter a lot one way or the other.
If the SQL were set up in a T(ADO)Query component on a DataModule and prepared there so that it was sitting around waiting to be used or if it were set up outside of a loop and then the parameters set and the query executed multiple times within the loop, your method would be much more efficent.  
In fact, if it were set up in a stored procedure, then that would be still more efficient.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Typecasting TBytes to Integer in Delphi XE8 2 62
SQL Rewrite without the NULLIF 4 33
QuickReport, Delphi, Shape 12 46
TAction.OnAfterExecute? 2 31
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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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