Friday, August 23, 2013

Simply Query WHERE in Special scenario

Today I got to know the most effective way to query the result from WHERE condition in a special scenario.

For example:
If you want to get the result set from Employee table
 When Select All Department or When Select One Department (only two options are there)

 You have to consider only WHERE column = <some value>
 If select All no need to worry about WHERE column IN (<all values>) or else IF ELSE or SWITCH Conditions

 Just simple you can get the result via OR logic
 Remember only one statement or both statement has to be TRUE to return in OR gate.


WHERE DepartmentId in (@Dep)  OR @Dep in (0)
when select all no value passes. So if pass 0 the logic change as
WHERE DepartmentId in (0)  OR @Dep in (0) -> TRUE OR TRUE

Look how it simple. :)

No comments:

Post a Comment