TSQL Case in where statement if parameter is null

前端 未结 4 1228
失恋的感觉
失恋的感觉 2021-02-06 01:09

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          


        
4条回答
  •  心在旅途
    2021-02-06 01:45

    I think this will give you what you're after.

    IF (@reportedByUser IS NULL)
        select * from table 
        where MadeByUserId = @madeByUserId
    ELSE
        select * from table 
        where MadeByUserId = @madeByUserId and ReportedByUserID = @reportedByUserID)
    

提交回复
热议问题