简体   繁体   English

如果全选被选中,SSRS避免使用WHERE子句

[英]SSRS avoid WHERE clause if select all is selcted

I am working on an SSRS report and a part of my sql query is like 我正在处理SSRS报告,我的SQL查询的一部分就像

WHERE SuperVisorId IN (@SupervisorIDs) AND CreatedDate> @StartDate

where the @SupervisorIDs is a dropdown with option of "select all" and individual supervisors. 其中@SupervisorIDs是带有“全选”选项和单个主管的选项的下拉列表。

So if the supervisors "all" option is selected , then i dont need to include that in the where clause and my where clause is only this 因此,如果选择了主管“ all”选项,那么我就不需要在where子句中包含它,而我的where子句仅是这个

  WHERE CreatedDate> @StartDate

So how can i make the WHERE clause looks different according to Selection of dropdown 那么如何根据选择下拉列表使WHERE子句看起来不同

This only applies if you are using a single valued parameter with a manually added All option to the list of available values. 仅当您使用单个值参数并将手动添加的“ All选项添加到可用值列表时,此选项才适用。 Multi-value parameters do not know when all options are selected. 多值参数不知道何时选择所有选项。

SQL Server doesn't always execute the conditions in a where clause in the order you write them, so if you are using where (@p = 'all' or col = @p) and ... you may still be comparing your values. SQL Server并不总是按照编写它们的顺序在where子句中执行条件,因此,如果您使用where (@p = 'all' or col = @p) and ...您可能仍在比较值。

If performance is a concern, you can avoid this by using a short circuiting case , that only progresses to the actual data comparison if it is necessary: 如果需要考虑性能,可以使用短路case来避免这种case ,只有在必要时才进行实际数据比较:

where case when @SupervisorIDs = 'All' then 1
           else case when SuperVisorId = @SupervisorIDs then 1
                     else 0
                     end
           end = 1
  and CreatedDate > @StartDate

Assuming that you are using a dataset query to populate the supervisor parameter dropdown, then you can try this. 假设您正在使用数据集查询来填充主管参数下拉列表,则可以尝试此操作。

Create an additional hidden parameter of a boolean type. 创建另一个布尔类型的隐藏参数。 For this example, I'll call it @AllSupsSelected. 对于此示例,我将其称为@AllSupsSelected。 Set the default value of the parameter to: =COUNT(Parameters!SupervisorIds.Label)=COUNT(Fields!SupervisorIdLabel.Value,"SupervisorDataset") 将参数的默认值设置为:= COUNT(Parameters!SupervisorIds.Label)= COUNT(Fields!SupervisorIdLabel.Value,“ SupervisorDataset”)

Replace the field and dataset names accordingly. 相应地替换字段和数据集名称。 If the dataset is returning non-distinct values, you may have to tinker further to get this working. 如果数据集返回的是不同的值,则可能需要进一步修补才能使此工作正常。

Now your query can read: 现在您的查询可以读取:

WHERE @AllSupsSelected OR SupervisorId IN (@SupervisorIds) WHERE @AllSupsSelected或SupervisorId IN(@SupervisorIds)

Make your where clause like below 使您的where子句如下所示

WHERE (
        (SuperVisorId IN (@SupervisorIDs))
        OR (
            @SupervisorIDs = 0
            AND COLUMN_WITH_NULL IS NOT NULL
            )
        )
    AND CreatedDate > @StartDate

And pass 0 when selected "select all" 并在选择“全选”时传递0

As an actual answer to your particular problem, set your multi-valued parameter dataset up similar to this to return all Supervisors as well as a value at the bottom of the list for No Supervisor : 作为对特定问题的实际答案,请设置多值参数数据集与此类似,以返回所有Supervisors以及列表的底部No Supervisor

select distinct SupervisorID as Value
               ,SupervisorName as Label
               ,1 as Sort
from Suppliers

union all

select <Uniquely identifiable value with the same data type as SupervisorID> as Value
      ,'No Supervisor' as Label
      ,2 as Sort

order by Sort
        ,Label

Then in your dataset set up your filtering similar to the below. 然后在您的数据集中设置与下面类似的过滤。 I have structured it in this manner to avoid using the isnull function on your SupervisorID column, which will hurt the query performance: 我以这种方式构造它,以避免在您的SupervisorID列上使用isnull函数,这会影响查询性能:

select cols
from tables
where SupervisorID in(@SupervisorID)
   or (SupervisorID is null
       and <Unique value from above> in (@SupervisorID)
      )

which version of ssrs ? 哪个版本的ssrs? in 2016, you don't need to alter your query. 在2016年,您无需更改查询。 when you click "select all" by default it pass all the values. 默认情况下,当您单击“全选”时,它将传递所有值。 so your query works good without changing anything. 因此您的查询在不进行任何更改的情况下效果很好。

thanks, SK 谢谢,SK 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM