May not have been the best title but anyways..
I have a stored procedure which until now had 1 optional parameter which I easily solved with 1 if statement. Now it has 2 which brings it up to 4 if statements. I would like to know if there is a better way to write a query then the following. Especially if/when a variable count goes up to 4 or 5.
I am looking for an easier way to add this new ignoreTheseIds variable without having to add 2 more if/else statements.
ALTER PROCEDURE [dbo].[SomeQuery]
@StartingDate varchar(10) = '1/1/1900',
@EndingDate varchar(10) = NOW,
@LimitToTheseIds varchar(MAX) = ''
@IgnoreTheseIds varchar(MAX) = ''
AS
BEGIN
SET NOCOUNT ON;
IF @LimitTo = ''
BEGIN
SELECT id1, col2
FROM Table1 as T
WHERE SomeDateTime <= @EndDate
AND SomeDateTime >= @StartDate
END
ELSE
BEGIN
SELECT id1, col2
FROM Table1 as T
WHERE SomeDateTime <= @EndDate
AND SomeDateTime >= @StartDate
AND @LimitToTheseIds LIKE '%|' + CAST(id1 as varchar) + '|%'
END
END
Hope I didn't mess anything up when changing the var names...