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!

CASE Statement or ? 1

Status
Not open for further replies.

Countymnca

Technical User
Jul 18, 2006
44
US
Hello,
I am having a very hard time trying to get a problem resolved. I am pretty sure a CASE Statement is what I need to use in the WHERE clause of my Query, but I am not getting it to work. Below is an explaination of what I need to the query to do.

I work for a pretty large organization and am moving various rosters and phone lists into a web app. Each of our various offices existing in a LOCATIONS table including the headquarters. I can pass the @currentuserID into the Where statement to limit the results to just employees or numbers at the officer where that employee works. The problem is I would like to have the same sp return all of the offices information when someone at headquarters runs the report. My query looks like:

SELECT A,B,C,.....
From Personnel
WHERE Office_ID IN (Select Office_ID from Personnel where @currentuserID = Personnel.EmployeeID)

I can also get it to work by passing the Office Name into the query and using LIKE (Select....)

The Office_ID of those assigned to headquarters is 17 so I thought maybe something with 17 as the ELSE result would work, but so far no luck.

I will admit to 0 experience with the CASE Statement as I come from the Access world and am use to IF, THEN and ELSE function.

Any suggestions or assistance would be greatly appreciated. If this is possible, I can use this statement as almost another rights level and that would be amazing.


 
Code:
Declare @Office_ID as Int

Select @Office_ID = Office_ID 
From Personnel 
Where Personnel.EmployeeID = @currentuserID;

/*
One Way
IF @Office_ID = 17 Then
SELECT A,B,C,.....
From Personnel;
Else
SELECT A,B,C,.....
From Personnel
WHERE Office_ID = @Office_ID;
*/

--Another way
SELECT A,B,C,.....
From Personnel
WHERE @Office_ID = 17 Or Office_ID = @Office_ID 
Option(Recompile);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top