Saturday, April 11, 2015

In WHERE clause when filter value is ZERO then Select all values from table in SSMS

For example i have a table called TeacherMaster

id    Name         Dept         Salary
---------------------------------------------
1   Pritam Das     1        20000.00
2   Nilima sen      2        18000.00
3   Lisa Das         1        30000.00
4   Rina Das        1        25000.00
5   Yubaraj S       2        16000.00
6   Arabind S       3        17000.00
Now i am supplying value through parameter in where clause
Here the query
Declare @Deptid int=1
select * from [dbo].[TeacherMaster] where Deptid=@Deptid or @Deptid=0
Now if i pass 1 in @Deptid Parameter then this will give output given below.
id    Name         Dept         Salary
—————————————-
1   Pritam Das     1        20000.00
3   Lisa Das         1        30000.00
4   Rina Das        1        25000.00
But, when i pass 0 in @Deptid Parameter then this will give all the table values as output given below.
Declare @Deptid int=0
select * from [dbo].[TeacherMaster] where Deptid=@Deptid or @Deptid=0
id    Name         Dept         Salary
—————————————-
1   Pritam Das     1        20000.00
2   Nilima sen      2        18000.00
3   Lisa Das         1        30000.00
4   Rina Das        1        25000.00
5   Yubaraj S       2        16000.00
6   Arabind S       3        17000.00
By this way we can achieve filter (Dropdown) along with ‘ALL’ option.

No comments:

Post a Comment