Combine 'like' and 'in' in a SqlServer Reporting Services query?

时光怂恿深爱的人放手 提交于 2019-12-12 20:03:43

问题


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.


回答1:


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.




回答2:


Have you tried to do:

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




回答3:


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




回答4:


put like this:

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



回答5:


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.



来源:https://stackoverflow.com/questions/262075/combine-like-and-in-in-a-sqlserver-reporting-services-query

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