My question is can I do the functions of these two selects in a single statement and get rid of the IF?
DECLARE @recID INT;
--Case 1
SET @recID = null;
--Case 2
--SET @recID = 117;
IF @recID is null
BEGIN
select * from myTable WHERE [myIDcolumn] is null -- works when recID is null
END
ELSE
BEGIN
select * from myTable WHERE [myIDcolumn] = @recID -- works when recID is number
END
How ’bout this:
If
@recIDisnull, the first part will never be true but the second part will be if[myIDcolumn]isnull, which covers thenullcase. If@recIDis notnull, the first part will match when appropriate (and the second part will be ignored). So both cases are covered.