Solved

Case in Where clause help

Posted on 2009-07-10
12
227 Views
Last Modified: 2012-05-07
I keep getting an error at the ">" in this statement.  What am I doing wrong?

Thaks,
-Matt
select vUser_Info.UserID, First_Name, Last_Name, EMail, vUser_Roles.Role_Name, Status, Corporate_Name

from dbo.vUser_Info, dbo.vCorporates, dbo.vUser_Roles

where vUser_Info.Corporate = vCorporates.CorporateID

and vUser_Info.RoleID = vUser_Roles.RoleID

and vCorporates.CorporateID = 3

and vUser_Info.RoleID = case 

when @RoleID = 0 then vUser_Info.RoleID > 0

when @RoleID <> 0 then vUser_Info.RoleID = vUser_Info.RoleID

end

order by vUser_Info.UserID

Open in new window

0
Comment
Question by:mattkovo
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 17

Accepted Solution

by:
pssandhu earned 400 total points
Comment Utility
Try it this way:
select vUser_Info.UserID, First_Name, Last_Name, EMail, vUser_Roles.Role_Name, Status, Corporate_Name

from dbo.vUser_Info, dbo.vCorporates, dbo.vUser_Roles

where vUser_Info.Corporate = vCorporates.CorporateID

	and vUser_Info.RoleID = vUser_Roles.RoleID

	and vCorporates.CorporateID = 3

	and (case when @RoleID = 0 then vUser_Info.RoleID > 0

		when @RoleID <> 0 then vUser_Info.RoleID = vUser_Info.RoleID

		end)

order by vUser_Info.UserID

Open in new window

0
 
LVL 17

Expert Comment

by:pssandhu
Comment Utility
Oopss... sorry thtas wrong. Following up...
0
 
LVL 17

Expert Comment

by:pssandhu
Comment Utility
Can you please explain what are you trying to achieve with the CASE statement?
P.
0
 

Author Comment

by:mattkovo
Comment Utility
I am trying to display a list of items based on the variable passed. I could use the case statement in a stored procedure to determine the select statement to use. But...I'd rater use one statement if possible.
0
 
LVL 17

Expert Comment

by:pssandhu
Comment Utility
So would something like this work for you:
IF @RoleID = 0

	BEGIN

		select	vUser_Info.UserID, 

				First_Name, Last_Name, 

				EMail, 

				vUser_Roles.Role_Name, 

				Status, 

				Corporate_Name

		from	dbo.vUser_Info, dbo.vCorporates, dbo.vUser_Roles

		where	vUser_Info.Corporate = vCorporates.CorporateID

				and vUser_Info.RoleID = vUser_Roles.RoleID

				and vCorporates.CorporateID = 3

				and vUser_Info.RoleID > 0

		order by vUser_Info.UserID

	END

ELSE

	BEGIN

		select	vUser_Info.UserID, 

				First_Name, Last_Name, 

				EMail, 

				vUser_Roles.Role_Name, 

				Status, 

				Corporate_Name

		from	dbo.vUser_Info, dbo.vCorporates, dbo.vUser_Roles

		where	vUser_Info.Corporate = vCorporates.CorporateID

				and vUser_Info.RoleID = vUser_Roles.RoleID

				and vCorporates.CorporateID = 3

		order by vUser_Info.UserID

	END

Open in new window

0
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 25 total points
Comment Utility
Maybe pssandhu has a lock on your problem but I must admit I still don't understand the requirements.

If @RoleID = 0 then what?

If @RoleID is not 0 then what?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 17

Expert Comment

by:pssandhu
Comment Utility
Yes, it is a bit confusing. But when I look at his CASE statment he wants to pick the role ids that are greater than 0 when the variable value is 0 and if not then roleid = roleid, which to me means that it does not matter.
May be I am reading it wrong as well!! :-)
P.
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 75 total points
Comment Utility
It looks like you just need this:
select vUser_Info.UserID, First_Name, Last_Name, EMail, vUser_Roles.Role_Name, Status, Corporate_Name

from dbo.vUser_Info, dbo.vCorporates, dbo.vUser_Roles

where vUser_Info.Corporate = vCorporates.CorporateID

and vUser_Info.RoleID = vUser_Roles.RoleID

and vCorporates.CorporateID = 3

and (@RoleID <> 0 OR vUser_Info.RoleID > 0)

order by vUser_Info.UserID

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
That is how I am reading it too P.
0
 

Author Comment

by:mattkovo
Comment Utility
P.  You are correct as what I'm trying to accomplish.  I think I found a way to do it but will get back soon. Thanks all!
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
It is a simple statement if that is what you want to do.  Don't need a CASE at all.  See http:#24826646.
0
 

Author Closing Comment

by:mattkovo
Comment Utility
I used a combo of ideas and came up with a different way to solve the problem.  Thanks guys!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now