Answered by:
how to Write query in better way

Question
-
Hi I have added All parameter report dropdown using Union so my Dropdown with Employee names like this - 'ALL','Mark','Sam'..... select * from dept where emp=@emp this works great but when somebody selects 'ALL' then it is not dispalying any record because it is not having any employee name with all but practically i want to display all employye at that time.I know I can do with If else but is there any way in single query I can manage both?Tuesday, December 6, 2011 7:22 AM
Answers
-
Alternatively, you could dynamically append WHERE clause within the report code (or front end) based on value selected in report and pass on the appropriate query to the server accordingly.
Thanks!
- Proposed as answer by Naomi N Tuesday, December 6, 2011 2:11 PM
- Marked as answer by Stephanie Lv Tuesday, December 13, 2011 9:23 AM
Tuesday, December 6, 2011 7:41 AM
All replies
-
One way of achieving this is as mentioned below. Assuming @emp would be declared and passed accordingly.
SELECT * FROM dept WHERE EMP = CASE WHEN @emp = 'ALL' THEN EMP ELSE @emp END
Another way of writing it could be as below:
SELECT * FROM dept WHERE ( ( @emp = 'ALL' AND 1 = 1 ) OR ( EMP = @emp ) )
Thanks!- Edited by Deepak MunigelaMicrosoft contingent staff Tuesday, December 6, 2011 7:31 AM
Tuesday, December 6, 2011 7:27 AM -
You can try this:
IF (@Param = 'All')
BEGIN
SELECT * FROM TableName
END
ELSE
SELECT * FROM TableName WHERE Name = @Param
END
Shatrughna.- Proposed as answer by Naomi N Tuesday, December 6, 2011 2:11 PM
Tuesday, December 6, 2011 7:28 AM -
shatrugna/Deepak I alredy mentioned that i want without if elseTuesday, December 6, 2011 7:30 AM
-
As you need to manage the output based on @emp passed, you need to include some kind of check.
Using CASE, you are not actually writing multiple queries. Rather you are setting what to equate to EMP column of dept - whether will it be EMP itself or @emp parameter - which gets decided based on what is being passed.
Hope this clarifies.
Thanks!
Tuesday, December 6, 2011 7:37 AM -
Alternatively, you could dynamically append WHERE clause within the report code (or front end) based on value selected in report and pass on the appropriate query to the server accordingly.
Thanks!
- Proposed as answer by Naomi N Tuesday, December 6, 2011 2:11 PM
- Marked as answer by Stephanie Lv Tuesday, December 13, 2011 9:23 AM
Tuesday, December 6, 2011 7:41 AM -
Take a moment to check any of the below mentioned blogs. The suggestion with IF ELSE is not a bad one for your case.
Do you use ISNULL(...). Don't, it does not perform - short blog by Denis Gobo
Dynamic Search Conditions in T-SQL Version for SQL 2008 (SP1 CU5 and later) - long and comprehensive article by Erland Sommarskog
Catch All Queries - short blog by Gail Shaw
Sunday T-SQL tip: How to select data with unknown parameter set Nice blog by Dmitri KorotkevitchOption recompile
Option recompile discussion thread
For every expert, there is an equal and opposite expert. - Becker's Law
My blogTuesday, December 6, 2011 2:13 PM -
select * from dept where @emp IN ('ALL', emp);
Tuesday, December 6, 2011 2:32 PM