Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with select statement 1

Status
Not open for further replies.

chilly442

Technical User
Jun 25, 2008
151
US
If I run the following SELECT statement it works until the "OR". It returns selected unit, accepted, notification, and OutageType = U1. If I add the "OR", then it also returns anything (different units, ones that are not accepted) that has a OutageType = U2.

SELECT *
FROM(Events)
WHERE(Unit = '" & Unit.Text & "')
AND (Accepted = '" & Accept & "')
AND (Notification = '" & Notify & "')
AND (OutageType = '" & OutageU1 & "')
OR (OutageType = '" & OutageU2 & "')
ORDER BY ID DESC"

Any ideas on fixing this so I get only the OutageType that meets the Unit, Accepted, Notification criteria?

I trade stars for help!
Thanks,
Chilly442

 
First of all, this is a SQL question, and should be posted in the correct forum.
Second, you should not use in-line SQL statments, you should use stored procedures with parameters. Look up sql-injection, and you will see why.
Third, to help you with the problem, it is a logic problem:

If I understand your requirements correctly, then this should work (psuedo code)
--Also, you should never use Select *, use the column names.
Code:
Select *
From Events
Where Unit = Critera AND
      Accepted = Criteria AND
      Notification = Criteria AND
      [red]([/red]OutageType = Critera1 OR OutageType = Critera2[red])[/red]
Notice the grouping of the logic with the parens...
 
The reason that I posted here was that I have my statement populating a dataset, and didn't know if that was going to turn out to be the problem.

Your sintax works perfectly.
Sorry for the mispost.
Thanks,
Chilly442
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top