Option 1:
CREATE PROCEDURE [dbo].[GetStudents]
@MinimumAge int = NULL
AS
select * from Students s where @MinimumAge is null or s.Age >= @MinimumAge
Option 2:
CREATE PROCEDURE [dbo].[GetStudents]
@MinimumAge int = NULL
AS
IF @MinimumAge IS NULL
BEGIN
select * from Students s
END
ELSE
BEGIN
select * from Students s where s.Age >= @MinimumAge
END
Will option 1 be slower than option 2 because it has extra WHERE
clause? Or will SQL Server take care of that ?
I think option 1 is good as I don't have to duplicate the code, unless it's slower. The original procedure has many lines of code. So I don't want to duplicate all code just for one condition, unless it's the only good option.
There is indexing on the age
column and it does not allow nulls.
The problem is, Actual SP is having many lines of code - So I don't want to duplicate all code just to add one where condition, unless it's the only good option.