结合“喜欢” 和“在”; 在 SqlServer Reporting Services 查询中?

发布于 2024-07-08 00:48:04 字数 187 浏览 9 评论 0原文

以下内容不起作用,但我正在寻找类似的东西。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弱骨蛰伏 2024-07-15 00:48:04

关于如何将 LIKE 运算符与参数一起使用,有几个选项。

选项 1

如果将 % 添加到参数值,则可以自定义 LIKE 过滤器的处理方式。 例如,您的查询可以是:

 SELECT name
 FROM master.dbo.sysobjects
 WHERE name LIKE @ReportParameter1

为了使数据集正确使用 LIKE 语句,那么您可以使用像 sysa% 这样的参数值。 当我使用此代码在 SSRS 2008 中测试示例报告时,我返回了以下四个表:

 sysallocunits
 sysaudacts
 sysasymkeys
 sysaltfiles

选项 2

另一种不需要用户添加任何“%”符号的方法是生成一个具有代码的变量并执行变量。

 DECLARE @DynamicSQL NVARCHAR(MAX) 

 SET @DynamicSQL = 
 'SELECT  name, id, xtype
 FROM dbo.sysobjects
 WHERE name LIKE ''' + @ReportParameter1 + '%''
 '

 EXEC (@DynamicSQL)

这将使您更好地控制如何使用 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:

 SELECT name
 FROM master.dbo.sysobjects
 WHERE name LIKE @ReportParameter1

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:

 sysallocunits
 sysaudacts
 sysasymkeys
 sysaltfiles

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.

 DECLARE @DynamicSQL NVARCHAR(MAX) 

 SET @DynamicSQL = 
 'SELECT  name, id, xtype
 FROM dbo.sysobjects
 WHERE name LIKE ''' + @ReportParameter1 + '%''
 '

 EXEC (@DynamicSQL)

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.

佞臣 2024-07-15 00:48:04

您是否尝试过执行以下操作:

从描述类似 (@SearchedDescription + '%') 的产品中选择 *
(在 % 符号两边加上单引号?)

Have you tried to do:

select * from Products where Description like (@SearchedDescription + '%')
(Putting single quotes around the % sign?)

深者入戏 2024-07-15 00:48:04

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....

蓝眼睛不忧郁 2024-07-15 00:48:04

像这样说:

select * 
from tsStudent 
where studentName like @SName+'%'

put like this:

select * 
from tsStudent 
where studentName like @SName+'%'
烏雲後面有陽光 2024-07-15 00:48:04

我知道这已经非常旧了,但是在我的搜索中出现了这个问题来解决同样的问题,我最终使用了此处未描述的解决方案。 我正在添加一个新的潜在解决方案来帮助其他可能关注的人。

正如所写,此解决方案仅适用于 SQL Server 2016 及更高版本,但可以通过编写自定义 string_split UDF,并使用子查询而不是 CTE。

首先,使用 JOIN 将 @SearchedDescription 作为单个字符串映射到数据集中:

=JOIN(@SearchedDedscription, ",")

然后使用 STRING_SPLIT 映射您的“A,B,C,D”类型字符串转换为表格结构。

;with
SearchTerms as (
  select distinct
    Value
  from 
    string_split(@SearchedDescription, ',')
)
select distinct
  *
from
  Products
  inner join SearchTerms on
    Products.Description like SearchTerms.Value + '%'

如果有人多次添加相同的搜索词,结果集中会出现重复的行。 同样,单个产品可以匹配多个搜索词。 我已将 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.

;with
SearchTerms as (
  select distinct
    Value
  from 
    string_split(@SearchedDescription, ',')
)
select distinct
  *
from
  Products
  inner join SearchTerms on
    Products.Description like SearchTerms.Value + '%'

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文