结合“喜欢” 和“在”; 在 SqlServer Reporting Services 查询中?
以下内容不起作用,但我正在寻找类似的东西。
select *
from Products
where Description like (@SearchedDescription + %)
SSRS 在参数前面使用 @ 运算符来模拟“in”,并且我没有找到将字符串与字符串列表匹配的方法。
The following doesn't work, but something like this is what I'm looking for.
select *
from Products
where Description like (@SearchedDescription + %)
SSRS uses the @ operator in-front of a parameter to simulate an 'in', and I'm not finding a way to match up a string to a list of strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于如何将 LIKE 运算符与参数一起使用,有几个选项。
选项 1
如果将 % 添加到参数值,则可以自定义 LIKE 过滤器的处理方式。 例如,您的查询可以是:
为了使数据集正确使用 LIKE 语句,那么您可以使用像 sysa% 这样的参数值。 当我使用此代码在 SSRS 2008 中测试示例报告时,我返回了以下四个表:
选项 2
另一种不需要用户添加任何“%”符号的方法是生成一个具有代码的变量并执行变量。
这将使您更好地控制如何使用 LIKE 语句。 如果您不希望用户注入任何其他运算符,那么您始终可以添加代码来删除非字母数字字符,然后再将其合并到最终查询中。
选项 3
您可以创建一个控制此功能的存储过程。 我通常更喜欢使用存储过程作为 SSRS 的数据源,并且从不允许动态生成 SQL,但这只是我的偏好。 这有助于在执行依赖性分析检查时提高可发现性,并且还允许您确保最佳的查询性能。
选项 4
创建有助于动态生成 SQL 代码的 .NET 代码程序集。 我认为这太过分了,充其量也是一个糟糕的选择,但它可以想象得到。
There are a few options on how to use a LIKE operator with a parameter.
OPTION 1
If you add the % to the parameter value, then you can customize how the LIKE filter will be processed. For instance, your query could be:
For the data set to use the LIKE statement properly, then you could use a parameter value like sysa%. When I tested a sample report in SSRS 2008 using this code, I returned the following four tables:
OPTION 2
Another way to do this that doesn't require the user to add any '%' symbol is to generate a variable that has the code and exceute the variable.
This will give you finer controller over how the LIKE statement will be used. If you don't want users to inject any additional operators, then you can always add code to strip out non alpha-numeric characters before merging it into the final query.
OPTION 3
You can create a stored procedure that controls this functionality. I generally prefer to use stored procedures as data sources for SSRS and never allow dynamically generated SQL, but that's just a preference of mine. This helps with discoverability when performing dependency analysis checks and also allows you to ensure optimal query performance.
OPTION 4
Create a .NET code assembly that helps dynamically generate the SQL code. I think this is overkill and a poor choice at best, but it could work conceivably.
您是否尝试过执行以下操作:
从描述类似 (@SearchedDescription + '%') 的产品中选择 *
(在 % 符号两边加上单引号?)
Have you tried to do:
select * from Products where Description like (@SearchedDescription + '%')
(Putting single quotes around the % sign?)
Dano,您使用的是哪个版本的 SSRS? 如果是RS2000,多参数列表为
没有官方支持,但有一个解决方法......
Dano, which version of SSRS are you using? If it's RS2000, the multi-parameter list is
not officially supported, but there is a workaround....
像这样说:
put like this:
我知道这已经非常旧了,但是在我的搜索中出现了这个问题来解决同样的问题,我最终使用了此处未描述的解决方案。 我正在添加一个新的潜在解决方案来帮助其他可能关注的人。
正如所写,此解决方案仅适用于 SQL Server 2016 及更高版本,但可以通过编写自定义 string_split UDF,并使用子查询而不是 CTE。
首先,使用 JOIN 将 @SearchedDescription 作为单个字符串映射到数据集中:
=JOIN(@SearchedDedscription, ",")
然后使用 STRING_SPLIT 映射您的“A,B,C,D”类型字符串转换为表格结构。
如果有人多次添加相同的搜索词,结果集中会出现重复的行。 同样,单个产品可以匹配多个搜索词。 我已将
distinct
添加到 SearchTerms CTE 和主查询中,以尝试抑制这种不适当的行重复。如果您的查询更复杂(包括其他联接的结果),那么这可能会成为一个越来越大的问题。 请注意,这是此方法的主要缺点。
I know this is super old, but this came up in my search to solve the same problem, and I wound up using a solution not described here. I'm adding a new potential solution to help whomever else might follow.
As written, this solution only works in SQL Server 2016 and later, but can be adapted for older versions by writing a custom string_split UDF, and by using a subquery instead of a CTE.
First, map your @SearchedDescription into your Dataset as a single string using JOIN:
=JOIN(@SearchedDedscription, ",")
Then use STRING_SPLIT to map your "A,B,C,D" kind of string into a tabular structure.
If someone adds the same search term multiple times, this would duplicate rows in the result set. Similarly, a single product could match multiple search terms. I've added
distinct
to both the SearchTerms CTE and the main query to try to suppress this inappropriate row duplication.If your query is more complex (including results from other joins) then this could become an increasingly big problem. Just be aware of it, it's the main drawback of this method.