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

后端 未结 8 1554
暖寄归人
暖寄归人 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:15

    I think this will help

    @id 
    @name
    
    SELECT [Id],[Name] FROM [Person] 
    WHERE Id = @id and ISNULL(@name, Name)
    

    This will allow you just ignore the Name condition if it is null

提交回复
热议问题