I have a Stored Procedure that is going to have 3 parameters. I’m wanting to check every combination of Null/Not Null but instead of writing out 16 different Select statements with Else If conditions how else could I write it? Here is my example currently…
...
ELSE IF @team IS NOT NULL AND
@position IS NULL AND
@filter IS NOT NULL
BEGIN
SELECT P1.PlayerKey, P1.Player, P2.Position, P1.Height, P1.Weight, P1.Speed, P1.Status
FROM Player AS P1
INNER JOIN Position AS P2 ON P1.PositionID = P2.PositionID
INNER JOIN Team AS T1 ON T1.TeamID = P1.TeamID
WHERE P1.TeamID = @team AND P1.Player LIKE '%' + @filter + '%'
ORDER BY P2.PosSort;
END
ELSE IF @team IS NULL AND
@position IS NOT NULL AND
@filter IS NOT NULL
BEGIN
SELECT P1.PlayerKey, P1.Player, P2.Position, P1.Height, P1.Weight, P1.Speed, P1.Status
FROM Player AS P1
INNER JOIN Position AS P2 ON P1.PositionID = P2.PositionID
INNER JOIN Team AS T1 ON T1.TeamID = P1.TeamID
WHERE P2.Position = @position AND P1.Player LIKE '%' + @filter + '%'
ORDER BY P2.PosSort;
END
ELSE IF @team IS NOT NULL AND
@position IS NULL AND
@filter IS NOT NULL
BEGIN
SELECT P1.PlayerKey, P1.Player, P2.Position, P1.Height, P1.Weight, P1.Speed, P1.Status
FROM Player AS P1
INNER JOIN Position AS P2 ON P1.PositionID = P2.PositionID
INNER JOIN Team AS T1 ON T1.TeamID = P1.TeamID
WHERE P1.TeamID = @team AND P1.Player LIKE '%' + @filter + '%'
ORDER BY P2.PosSort;
END
...
1 Answer