首页 > 解决方案 > 如何在不相互依赖的where子句中添加多个参数

问题描述

我需要一个带有 5 个参数的查询,例如...

姓名,姓氏,出生地,出生日期,ID。

该参数应默认为 null 并且不相互依赖。运行报告时,我应该仅使用 ID 等进行过滤。有时我可以使用 2 个或更多参数进行过滤

标签: sqlsql-serverreporting-services

解决方案


SQL将沿着

Select
your columns

from some_table

where 

(@name = some_table.name_column or @name is null)
and
(@surname = some_table.surname_column or @surname is null)
and
(@birth_place = some_table.birth_place_column or @birth_place is null)
and
(@dateofbirth= some_table.dateofbirth_column or @dateofbirth is null)
and
(@ID= some_table.ID_column or @ID is null)

现在在 SSRS 中,允许您的所有参数为空值。当然.. 您需要选择至少一个参数值,否则您实际上是从表中返回所有内容.. 我敢肯定这是您不想要的。


推荐阅读