SQL Proceudre Syntax

I'm trying to create a simple procedure but for some reason the first 2 parts of the WHERE statement aren't working. What am I doing wrong?

SELECT dbo.joitem.fjobno, dbo.joitem.fpartno, dbo.joitem.fgroup, dbo.joitem.fmqty, dbo.joitem.fmeasure, dbo.joitem.fprodcl, dbo.joitem.fdesc, dbo.jodbom.fbompart, dbo.jodbom.fbomdesc, dbo.jodbom.fqty_iss FROM dbo.jodbom 
JOIN dbo.joitem ON dbo.joitem.fjobno = dbo.jodbom.fjobno WHERE (dbo.joitem.fprodcl = '01') OR (dbo.joitem.fprodcl = '04')  AND (dbo.joitem.fpartno <> 'INDIRECT LABOR') OR (dbo.joitem.fpartno <> 'NOT CLOCKED');

Open in new window

Lawrence SalvucciInformation Technology ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
You need extra parentheses around the first set of OR statements. Change the second OR to an AND.
WHERE ((dbo.joitem.fprodcl = '01') OR (dbo.joitem.fprodcl = '04'))
AND    (dbo.joitem.fpartno <> 'INDIRECT LABOR') AND (dbo.joitem.fpartno <> 'NOT CLOCKED')

Open in new window

However, that can be written the same way using the IN clause:
WHERE  dbo.joitem.fprodcl IN ('01', '04')
AND    dbo.joitem.fpartno NOT IN ('INDIRECT LABOR', 'NOT CLOCKED')

Open in new window

Shaun KlineLead Software EngineerCommented:
Your WHERE clause will return any joitem with an fprodcl that equals '01', or any fprodcl that equals '04' and has a  fpartno not equal to 'INDIRECT LABOR', or any  fpartno not equal to 'NOT CLOCKED'.

Best guess is that you want:

WHERE ((dbo.joitem.fprodcl = '01') OR (dbo.joitem.fprodcl = '04'))  AND ((dbo.joitem.fpartno <> 'INDIRECT LABOR') OR (dbo.joitem.fpartno <> 'NOT CLOCKED'));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
Thank you both for your quick responses!
dsackerContract ERP Admin/ConsultantCommented:
Actually, Shaun's response would be wrong, as combining OR..<> will make them clobber each other, and both values will fall through.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.