Hi,
I would like to build a where clause based upon parameters passed in to a stored procedure. Basically, each parameter corresponds to a column, and if a parameter is null or an empty string, I do not want it considered in the where clause (i.e. I want all values returned for its corresponding column).
Here is what I did as a quick and dirty fix, but now I'd like to do it the right way! Any help would be greatly appreciated!
CREATE PROCEDURE sprocGetMembers @isactive bit, @dept varchar(4),@lastname varchar(35) AS
SELECT *
FROM Member
WHERE IsActive LIKE CASE @isactive
WHEN NULL
THEN '%'
ELSE @isactive
END AND
EMPLOYEE_DEPARTMENT_NUMBER LIKE CASE @dept
WHEN ''
THEN '%'
ELSE @dept
END AND
LastName LIKE CASE @lastname
WHEN ''
THEN '%'
ELSE '%' + @lastname + '%'
END
GO
I would like to build a where clause based upon parameters passed in to a stored procedure. Basically, each parameter corresponds to a column, and if a parameter is null or an empty string, I do not want it considered in the where clause (i.e. I want all values returned for its corresponding column).
Here is what I did as a quick and dirty fix, but now I'd like to do it the right way! Any help would be greatly appreciated!
CREATE PROCEDURE sprocGetMembers @isactive bit, @dept varchar(4),@lastname varchar(35) AS
SELECT *
FROM Member
WHERE IsActive LIKE CASE @isactive
WHEN NULL
THEN '%'
ELSE @isactive
END AND
EMPLOYEE_DEPARTMENT_NUMBER LIKE CASE @dept
WHEN ''
THEN '%'
ELSE @dept
END AND
LastName LIKE CASE @lastname
WHEN ''
THEN '%'
ELSE '%' + @lastname + '%'
END
GO