How can I modify this stored procedure to also return rows with NULL column values?
@title VARCHAR(50) NULL,
@initials VARCHAR(100) NULL,
@surname VARCHAR(100) NULL,
AS
BEGIN
SELECT
ID, MEMBERSHIP_ID, TITLE, INITIALS, SURNAME,
FROM
MEMBERSHIP_DTLS
WHERE
TITLE = (CASE WHEN @title IS NULL THEN (TITLE) ELSE @title END)
AND INITIALS = (CASE WHEN @initials IS NULL THEN INITIALS ELSE @initials END )
AND SURNAME = (CASE WHEN @surname IS NULL THEN SURNAME ELSE @surname END)
END
GO
What I am trying to accomplish here, is that if a NULL parameter is passed to the procedure then the results returned won't be filtered by that parameter, so if all parameters were null then everything in that table is returned.
The above works only with column values that are not null when a null is passed to all parameters then nothing is returned, the actual stored procedure has many more parameters so I cannot go for a solution where I rewrite the expression to match each combination.
