如何查询参数是否为null(具有倍数参数,而当一个或倍数参数为null时,选择语句会更改)

发布于 2025-01-23 11:48:17 字数 766 浏览 3 评论 0原文

我正在基于具有倍数列的SQL表创建SSRS报告。 在我的报告中,我最多有10个参数。我想创建一个数据集,该数据集将填充我的报告基础上的参数(每个参数都是查询中的条件)。问题是我所有参数以外的参数都可能为null。 根据一些研究,我发现“如果嵌套”可以起作用,基本上有类似的东西:

if(@param1 IS NOT NULL)
{
   SELECT * from table
   WHERE Column_1=@param1  ;
}
else
if(@param1 IS NOT NULL AND @param2 IS NOT NULL)
{
  SELECT * from table where Colum_1=@param1 AND Column_2=@param2;
}
   else.....

但是我很快意识到,由于我有10个参数,因此如果我想涵盖所有情况,则有很多组合(如果)。例如,如果我认为3个参数不是null,则我将不得不添加一个if在(param1,param2,param3)不是null的情况下,另一个是(param1,param2,param4)不为null, (param1,param2,param5),(param1,param2,param6)....当4个params nes null n null时相同,当10个params不为null时,直到10个params并非null。

关于更好的方式,更少的耗时来构建此查询吗?

谢谢!

I am creating a SSRS report based on a SQL table which has multiples columns.
In my report I have up to 10 parameters. I want to create a dataset that will populate my report base on the parameters (each parameter being a condition in the query). The thing is that all my parameters except one can be NULL.
Based on some research, I figured out that "if nested" can work, basically having something like this:

if(@param1 IS NOT NULL)
{
   SELECT * from table
   WHERE Column_1=@param1  ;
}
else
if(@param1 IS NOT NULL AND @param2 IS NOT NULL)
{
  SELECT * from table where Colum_1=@param1 AND Column_2=@param2;
}
   else.....

But I quickly realize that since I have 10 params that is a lot of combinations (IF) to have if I want to cover all the cases. For example if I consider that 3 of the params are not null then I will have to add a IF for the case where (param1, param2, param3) are not null, another one when (param1, param2, param4) are not null, (param1, param2, param5), (param1, param2, param6).... Same when 4 params are not null, 5 params are not null all the way up to when 10 params are not null.

Any ideas on a better ways and less time consuming to build this query?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云巢 2025-01-30 11:48:17

您可以这样做很简单。

SELECT * FROM myTable 
    WHERE (Col1 = @p1 OR @p1 IS NULL)
      AND (Col2 = @p2 OR @p2 IS NULL)
      AND (Col3 = @p3 OR @p3 IS NULL)

等等...

You can do this quite simply like this.

SELECT * FROM myTable 
    WHERE (Col1 = @p1 OR @p1 IS NULL)
      AND (Col2 = @p2 OR @p2 IS NULL)
      AND (Col3 = @p3 OR @p3 IS NULL)

and so on...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文