我有一个存储过程,如下所示:
CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT = 1 AS SELECT * FROM dbo.SomeTable T WHERE T.Column1 IS NOT NULL AND CASE WHEN @AdditionalFilter = 1 THEN T.Column2 IS NOT NULL
不用说,这不起作用.如何激活检查@AdditionalFilter参数的附加where子句?谢谢你的帮助.
CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT = 1 AS SELECT * FROM dbo.SomeTable T WHERE T.Column1 IS NOT NULL AND (@AdditionalFilter = 0 OR T.Column2 IS NOT NULL)
如果@AdditionalFilter为0,则不会评估该列,因为它不会影响括号之间部分的结果.如果它不是0,则将评估列条件.