I have a SP that gives me a lot of hard times.
The sp gets a two parameters @madeByUserId and @reportedByUserId. I want to have something like:
select
You could use COALESCE
.
SELECT *
FROM Table
WHERE 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.