SQL Server 2008 - Conditional Query

前端 未结 6 1497
说谎
说谎 2020-12-10 19:09

SQL is not one of my strong suits. I have a SQL Server 2008 database. This database has a stored procedure that takes in eight int parameters. For the sake of keeping this q

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 19:49

    The pattern (column = @param OR @param IS NULL) will give you optional parameters. You can use NULLIF to neutralize your -1. Even better would be to allow null parameters, instead of using a magic number.

    WHERE
       (Customer.IsActive = NULLIF(@isActive, -1) OR NULLIF(@isActive, -1) IS NULL)
    

提交回复
热议问题