我之前已经问过这个问题,但没有解决方法。我创建了一个带有以下数据集的多值过滤器。
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)
如果我的accessVar
的值不使用通配符但我需要它,那将相对容易。因此,accessVar
中可能会有5个值:
%(means all), 'At%', 'Bet%', 'Co%' and 'Dea%'
我无法在带有通配符的In运算符中使用。其次,我可以选择这种下拉过滤器吗?如果选择了第n个,则只查询全部。
我还有其他选择吗?
答案 0 :(得分:1)
您希望将LIKE和多值参数一起使用,但这不起作用。但是,Reporting Services使我们能够执行几乎任何我们想要的操作。解决方案是使用自定义代码和表达式。首先,我们将您的SQL语句更改为表达式,如下所示:
="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)) "
所以现在SQL语句实际上是一个字符串表达式,它将计算一个将执行的SQL表达式。
接下来,我们需要将您的多值参数转换为一系列LIKE
语句,我们可以使用自定义代码。将以下自定义代码添加到报表中(右键单击报表,选择“属性”,然后单击“代码”选项卡):
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
然后我们将此函数称为SQL语句的一部分:
Code.AccessLocations(Parameters!accessvar)
所以你的完整SQL是:
="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)) "
如果您的参数中包含%
通配符,那么这将有效;否则将通配符添加到函数中。