[英]SSRS avoid WHERE clause if select all is selcted
我正在處理SSRS報告,我的SQL查詢的一部分就像
WHERE SuperVisorId IN (@SupervisorIDs) AND CreatedDate> @StartDate
其中@SupervisorIDs是帶有“全選”選項和單個主管的選項的下拉列表。
因此,如果選擇了主管“ all”選項,那么我就不需要在where子句中包含它,而我的where子句僅是這個
WHERE CreatedDate> @StartDate
那么如何根據選擇下拉列表使WHERE子句看起來不同
All
選項添加到可用值列表時,此選項才適用。 多值參數不知道何時選擇所有選項。 SQL Server並不總是按照編寫它們的順序在where
子句中執行條件,因此,如果您使用where (@p = 'all' or col = @p) and ...
您可能仍在比較值。
如果需要考慮性能,可以使用短路case
來避免這種case
,只有在必要時才進行實際數據比較:
where case when @SupervisorIDs = 'All' then 1
else case when SuperVisorId = @SupervisorIDs then 1
else 0
end
end = 1
and CreatedDate > @StartDate
假設您正在使用數據集查詢來填充主管參數下拉列表,則可以嘗試此操作。
創建另一個布爾類型的隱藏參數。 對於此示例,我將其稱為@AllSupsSelected。 將參數的默認值設置為:= COUNT(Parameters!SupervisorIds.Label)= COUNT(Fields!SupervisorIdLabel.Value,“ SupervisorDataset”)
相應地替換字段和數據集名稱。 如果數據集返回的是不同的值,則可能需要進一步修補才能使此工作正常。
現在您的查詢可以讀取:
WHERE @AllSupsSelected或SupervisorId IN(@SupervisorIds)
使您的where子句如下所示
WHERE (
(SuperVisorId IN (@SupervisorIDs))
OR (
@SupervisorIDs = 0
AND COLUMN_WITH_NULL IS NOT NULL
)
)
AND CreatedDate > @StartDate
並在選擇“全選”時傳遞0
作為對特定問題的實際答案,請設置多值參數數據集與此類似,以返回所有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
然后在您的數據集中設置與下面類似的過濾。 我以這種方式構造它,以避免在您的SupervisorID
列上使用isnull
函數,這會影響查詢性能:
select cols
from tables
where SupervisorID in(@SupervisorID)
or (SupervisorID is null
and <Unique value from above> in (@SupervisorID)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.