TSQL Case in where statement if parameter is null
Try:
select * from table where MadeByUserId = @madeByUserId AND (@reportedByUserID IS null OR ReportedByUserID = @reportedByUserID)
You could use COALESCE
.
SELECT * FROM TableWHERE MadeByUserId = @madeByUserId AND ReportedByUserID = COALESCE(@reportedByUserID, ReportedByUserID)
This translates to
if @reportedByUserID is `NOT NULL` then compare ReportedByUserID with @reportedByUserID else compare ReportedByUserID with ReportedByUserID
From MSDN
COALESCE
Returns the first nonnull expression among its arguments.
Add an if statement
IF (@reportedByUserId IS NOT NULL)
SELECT * FROM table tWHERE t.MadeByUserId = madeByUserId etc