简体   繁体   English

SSRS:如何使用具有多个值的Search参数?

[英]SSRS: How can I use a Search parameter with multiple values?

I am creating a report that the business would like to use to spot check 5, 10 or 20 records at a time. 我正在创建一个报告,该业务将用于一次抽查5、10或20条记录。

The request was to use a Search parameter (as opposed to dropdown params). 请求是使用Search参数(而不是下拉参数)。

How can I create a Search parameter that will allow for multiple values separated by a comma? 如何创建一个搜索参数,以允许多个值之间用逗号分隔?

Thanks for the help. 谢谢您的帮助。

Don't worry about the length of this answer, most of it is a cut/paste job ! 不用担心 这个答案的长度,大部分都是剪切/粘贴作业! I've tried to explain each bit as we go so you understand it better, the actual amount of code you need to craft is minimal. 我尽力解释每一步,以便您更好地理解,您需要编写的实际代码量很小。

If you have SQL Server 2016 then you can take advantage of the new string_split function, if you have an older version you'll have to create a similar function yourself, or copy the one I created a few years back which does a similar thing. 如果您有SQL Server 2016,则可以利用新的string_split函数,如果您有旧版本,则必须自己创建一个类似的函数,或者复制我几年前创建的一个函数,该函数也可以执行类似的操作。

Lets get the function created first: I've created it in the fn schema but you can obviously change this to whatever schema you like. 让我们先创建函数:我已经在fn模式中创建了它,但是您显然可以将其更改为所需的任何模式。

CREATE  FUNCTION [fn].[Split](@sText varchar(8000), @sDelim varchar(20) = ' ')
RETURNS @retArray TABLE (idx smallint Primary Key, value varchar(8000))
AS
BEGIN
DECLARE @idx smallint,
    @value varchar(8000),
    @bcontinue bit,
    @iStrike smallint,
    @iDelimlength tinyint
IF @sDelim = 'Space'
    BEGIN
    SET @sDelim = ' '
    END
SET @idx = 0
SET @sText = LTrim(RTrim(@sText))
SET @iDelimlength = DATALENGTH(@sDelim)
SET @bcontinue = 1
IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty'))
    BEGIN
    WHILE @bcontinue = 1
        BEGIN
--If you can find the delimiter in the text, retrieve the first element and
--insert it with its index into the return table.

        IF CHARINDEX(@sDelim, @sText)>0
            BEGIN
            SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1)
                BEGIN
                INSERT @retArray (idx, value)
                VALUES (@idx, @value)
                END

--Trim the element and its delimiter from the front of the string.
            --Increment the index and loop.
SET @iStrike = DATALENGTH(@value) + @iDelimlength
            SET @idx = @idx + 1
            SET @sText = LTrim(Right(@sText,DATALENGTH(@sText) - @iStrike))

            END
        ELSE
            BEGIN
--If you can't find the delimiter in the text, @sText is the last value in
--@retArray.
 SET @value = @sText
                BEGIN
                INSERT @retArray (idx, value)
                VALUES (@idx, @value)
                END
            --Exit the WHILE loop.
SET @bcontinue = 0
            END
        END
    END
ELSE
    BEGIN
    WHILE @bcontinue=1
        BEGIN
        --If the delimiter is an empty string, check for remaining text
        --instead of a delimiter. Insert the first character into the
        --retArray table. Trim the character from the front of the string.
--Increment the index and loop.
        IF DATALENGTH(@sText)>1
            BEGIN
            SET @value = SUBSTRING(@sText,1,1)
                BEGIN
                INSERT @retArray (idx, value)
                VALUES (@idx, @value)
                END
            SET @idx = @idx+1
            SET @sText = SUBSTRING(@sText,2,DATALENGTH(@sText)-1)

            END
        ELSE
            BEGIN
            --One character remains.
            --Insert the character, and exit the WHILE loop.
            INSERT @retArray (idx, value)
            VALUES (@idx, @sText)
            SET @bcontinue = 0  
            END
    END
END
RETURN
END

Once the function is created you can see what it outputs by doing something like 创建函数后,您可以通过执行以下操作来查看其输出

select * from fn.Split('Austria, Belgium, France', ',')

This returns the following 这将返回以下内容

idx value
0   Austria
1   Belgium
2   France

Lets assume we have a geography table with the names of countries and their associated region, we can search for matching entries by simply joining to the output of the function something like this. 假设我们有一个包含国家和地区名称的地理表,我们可以通过简单地将类似这样的函数的输出加入来搜索匹配的条目。

select g.CountryID, g.CountryDesc, g.ContinentDesc from dim.Geography g
    join (SELECT * FROM fn.Split('Austria, Belgium, France', ',')) s 
        on g.CountryDesc = s.value

This, in my case, give me this output. 就我而言,这就是输出。

CountryID   CountryDesc ContinentDesc
21  Austria West Europe
28  Belgium West Europe
89  France  West Europe

To use the split function in your SSRS dataset, simply pass in the search text parameter so the query would now look something like this. 要在您的SSRS数据集中使用split函数,只需传入search text参数,这样查询现在看起来像这样。

select g.CountryID, g.CountryDesc, g.ContinentDesc from dim.Geography g
    join (SELECT * FROM fn.Split(@MySearchText, ',')) s 
        on g.CountryDesc = s.value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SSRS(Visual Studios 2012)中的级联参数无法使用多个值 - Cascading parameter in SSRS(Visual Studios 2012) not working with multiple values SSRS 2012-将参数中的多个值发送到主数据集 - SSRS 2012 - Sending multiple values in parameter to main dataset 是否可以通过解决方案配置设置SSRS参数默认值? - Can I set SSRS parameter defaults by solution configuration? 如何使用参数值列表? - How to use a list of values for a parameter? 如何在SSRS表达式编辑器中使用箭头键和制表符 - How can I use the arrow keys and the tab character in SSRS expression editor 如何在visual studio中使用正则表达式的搜索替换? - How can I use search replace in visual studio with regular expression? SSRS:基于EndDate参数的StartDate参数的默认值 - SSRS: Default Values for StartDate Parameter Based on EndDate Parameter 如何获得此SSRS计算以返回正确的除法和 - How can I get this SSRS calculation to return the correct division sum 如何在SSRS-Visual Studio 2012中将数据集参数映射到报表参数? - How to map dataset parameter to report parameter in SSRS - Visual Studio 2012? SSRS订阅无法识别我定义的日期参数默认值 - SSRS subscription is not recognising a date parameter default that I have defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM