Multi-value optional filters with the usage of wildcard in SSRS 2005

末鹿安然 提交于 2019-12-14 03:29:43

问题


I have asked this question before but there are no solution to it. I have created a multivalue filter with the following data-set.

SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, ACCESS_LOCATION_X, IC_N, COMPANY_M,  CONSECUTIVE_D 
FROM TEMP_TARGET
WHERE (CONSECUTIVE_D >= @consecDays) AND (ENTRY_DT BETWEEN @startDate AND @endDate)               AND
(ACCESS_LOCATION_X LIKE @accessVar) AND
(IC_N LIKE @icVAr)  

It would be relatively easy if the value of my accessVar does not use wildcard but i needed that. So there will be 5 values possible in accessVar:

%(means all), 'At%', 'Bet%', 'Co%' and 'Dea%'

I am unable to use In operator with wildcard. Secondly, can i make this kind of dropdown filters optional? if nth is selected, just query all.

What other options do i have?


回答1:


You want to use LIKE and multi-value parameters together, which isn't going to work. However, Reporting Services gives us the ability to do almost anything we want. The solution is to use custom code and expressions. First, we change your SQL statement into an expression, like so:

="SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, ACCESS_LOCATION_X, IC_N, "
&"COMPANY_M, CONSECUTIVE_D "
&"FROM TEMP_TARGET "
&"WHERE (CONSECUTIVE_D >= @consecDays) "
&"AND (ENTRY_DT BETWEEN @startDate AND @endDate) "
&"AND ((@accessvar IS NULL) OR (ACCESS_LOCATION_X LIKE @accessVar)) "
&"AND ((@icVar IS NULL) OR (IC_N LIKE @icVAr)) "

So now the SQL statement is actually a string expression that will evaluate to a SQL expression which will execute.

Next we need to convert your multi-value parameter into a series of LIKE statements, which we can do with custom code. Add the following custom code to your report (right-click report, select Properties and click the Code tab):

Function AccessLocations (ByVal parameter As Parameter) AS String
  Dim Result As String
  If parameter.IsMultiValue then
    Result = "AND ( " 
    For i as integer = 0 to parameter.Count-1 
      Result = Result + "(ACCESS_LOCATION LIKE '" + CStr(parameter.Value(i)) + "') OR "  
    Next 
    Result = Left(Result, Result.Length - 3) +") "
  Else 
    Result = "AND (ACCESS_LOCATION LIKE '" + CStr(parameter.Value) + "') "
  End If 

  Return Result
End Function

Then we call this function as part of the SQL statement:

Code.AccessLocations(Parameters!accessvar)

So your full SQL is:

="SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, ACCESS_LOCATION_X, IC_N, "
&"COMPANY_M, CONSECUTIVE_D "
&"FROM TEMP_TARGET "
&"WHERE (CONSECUTIVE_D >= @consecDays) "
&"AND (ENTRY_DT BETWEEN @startDate AND @endDate) "
& Code.AccessLocations(Parameters!accessvar)
&"AND ((@icVar IS NULL) OR (IC_N LIKE @icVAr)) "

If your parameter has the % wildcard in it then this will work; otherwise add the wildcard to the function.



来源:https://stackoverflow.com/questions/13906944/multi-value-optional-filters-with-the-usage-of-wildcard-in-ssrs-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!