SQL Server在报表生成器

时间:2015-04-23 15:25:34

标签: sql-server reporting-services ssrs-2008 sql-like

我有一个报告,我试图允许用户从下拉列表中为报告生成器中的结果选择多个预定的LIKE值。有没有办法可以做到这一点?我曾尝试使用LIKE IN(),但这两个关键字似乎并不一致。这是我的代码。如果我选择一个选项,我的代码才有效。

DECLARE @Warehouse nvarchar(10)
DECLARE @Location nvarchar(10)
SET @Warehouse = 'Warehouse1'
SET @Location = 'IB'

SELECT Part
, Tag
, CurrentLocation AS 'Location'
, TotalQty
, DateTimeCreated 
, datediff(hour, DateTimeCreated, getdate()) AS 'Hours in Location'
, User AS 'Last User'
FROM table1
WHERE datediff(hour, DateTimeCreated, getdate())>=1
AND Warehouse IN(@Warehouse)
AND(CurrentLocation LIKE '%' + @Location + '%')
ORDER BY 'Hours in Location' DESC, CurrentLocation

2 个答案:

答案 0 :(得分:1)

这最好用存储过程处理。以下是它如何工作的高级描述。每种技术都可以通过一些精明的谷歌搜索在低级别学习:

对于报表数据集,调用存储过程,将多值参数传递给存储过程中的varchar参数。对于本答案的其余部分,我们将调用该参数@MVList

在存储过程中,@ MVList将作为用户在参数列表中选择的所有选项的逗号分隔字符串接收。

从Table1编写SELECT查询,加入分割@MVList(谷歌SQL分割函数以获取预编写代码)的表值函数,并为用户选择的每个值生成一行表。

对于JOIN条件,而不是等于,执行LIKE:

INNER JOIN MySplitFunction(@MVList, ',') AS SplitFunction
ON Table1.CurrentLocation LIKE '%'+SplitFunction.value+'%'

查询结果将是您要查找的IN / LIKE结果。

答案 1 :(得分:0)

感谢您的回复。这就是我最终解决的问题。

SELECT Part
, Tag
, CurrentLocation AS 'Location'
, TotalQty
, DateTimeCreated 
, datediff(hour, DateTimeCreated, getdate()) AS 'Hours in Location'
, User AS 'Last User'
FROM table 1
WHERE datediff(hour, DateTimeCreated, getdate())>=1
AND Warehouse in (@Warehouse)
AND LEFT(CurrentLocation,2) IN(@Location)
ORDER BY 'Hours in Location' DESC, CurrentLocation
相关问题