Checking an input param if not Null and using it in where in SQL Server

后端 未结 8 1541
暖寄归人
暖寄归人 2020-12-23 14:05

What is the best way to include an input param in the WHERE clause but exclude it if it is null?

There are a number of ways I believe, but I can\'t seem

相关标签:
8条回答
  • 2020-12-23 14:28

    You can use IsNull

     where some_column = IsNull(@yourvariable, 'valueifnull')
    

    EDIT:

    What you described in the comment can be done like:

    where (@code is null or code = @code)
    
    0 讨论(0)
  • 2020-12-23 14:29

    Here's another approach

    SELECT * FROM Thingies WHERE ( @thingId IS NULL OR ThingID = @thingId )
    
    0 讨论(0)
提交回复
热议问题