Books for asp.net

Sunday, May 15, 2011

MSSQL filter record with IF Statement

Example to filter user records by name / email if both criteria have the value

DECLARE @Name varchar(20)
DECLARE @Email varchar(20)

SET @Name = 'user'
SET @Email = ''

select * from User
WHERE (@Name = '' OR userName = @Name)
AND (@Email = '' OR userEmail= @Email)