Monday, June 24, 2013

Writing Single select query for multiple filter in sql where condition

In the stored produces some time we need to use multiple filters. Some have value and some don’t have any value. For that we usually write different IF condition to execute the select statement. In SQL have simple way to use one select statement to use multiple where condition or multiple filter.




CREATE Procedure [dbo].[YourSP]
@Empid varchar(50),
@Edept varchar(50)

As
Begin


IF @ Empid = ''
Set @ Empid = null

IF @ Edept= ''
Set @ Edept = null


select * from Employee Where ((@Empid is null or EmpidID = @ Empid) and (@Edept is null or EDept=@Edept))

End

No comments: