简体   繁体   English

在SQL查询中使用SSRS多值参数而不使用IN运算符

[英]Use an SSRS multi-value parameter in a SQL query without using the IN operator

I have to pass multiple values (eg: Year1, year2, year3, etc.) to the same query, but I cannot use the IN condition as I'm using less than or equal to in most of the cases. 我必须将多个值(例如:Year1,year2,year3等)传递给同一查询,但是由于在大多数情况下我使用小于或等于值,因此无法使用IN条件。 Can I do this by passing multiple values through the same parameter without changing the query? 我可以通过在不更改查询的情况下通过同一参数传递多个值来做到这一点吗?

Is it possible to get multiple values from an SSRS parameter and pass them on to the query to get the output as: 是否可以从SSRS参数获取多个值并将其传递给查询以获取输出,如下所示:

Year1           Year2           Year3
Value(output)   Value(output)   Value(output) 

You can pass the multi-value parameter as a comma separated string but your SQL query is going to need updating to handle that CSV string. 您可以将多值参数作为逗号分隔的字符串传递,但是您的SQL查询将需要更新以处理该CSV字符串。 In order to pass the multi-value parameter as a CSV string you would open the dataset properties and go to the parameters tab. 为了将多值参数作为CSV字符串传递,您可以打开数据集属性并转到“参数”标签。 Then set the value of your parameter to this expresssion: 然后将参数的值设置为此表达式:

=JOIN(Parameters!MultiValueYearParameter.Value,",")

This will join all of the values in the multivalue parameter together and use a comma as the delimiter. 这会将multivalue参数中的所有值连接在一起,并使用逗号作为定界符。 You can then process this using the split function below (or just modify it to work inline in your SQL if you either cannot or don't need to create a separate function to do this). 然后,您可以使用下面的split函数来处理此问题(或者,如果您不能或不需要创建单独的函数来执行此操作,则只需对其进行修改即可在SQL中内联工作)。

This blog post on a Split Function for T-SQL using FOR XML shows how to do this without using string parsing or a while loop. 这篇有关使用FOR XML进行T-SQL拆分功能的博客文章显示了如何在不使用字符串解析或while循环的情况下执行此操作。 String parsing is prone to error and isn't scalable and while loops should just be avoided in SQL whenever possible. 字符串解析容易出错,并且不可扩展,尽管应尽可能在SQL中避免循环。

Below I've modified the split function to return a table of DATE values that you can then use in an INNER JOIN to filter your query using whatever operators you like. 下面,我修改了split函数,以返回一个DATE值表,然后您可以在INNER JOIN使用该表使用您喜欢的任何运算符来过滤查询。

--this is the parameter passed from the report
--(the date strings may not be formatted this way. do not try to rely on that)
DECLARE @YearParameter VARCHAR(MAX) = '2014-01-01,2011-12-02,2015-10-22';


--use this to do the xml parsing
declare @xml xml = N'<root><r>' + replace(@YearParameter,',','</r><r>') + '</r></root>'

--create a table variable to store the date values
DECLARE @dateValues TABLE (val DATE);

--parse the xml/csv string and cast the results to a DATE and insert into the table var
INSERT INTO @dateValues
select CAST(r.value('.','varchar(max)') AS DATE) AS val
from @xml.nodes('//root/r') as records(r);

You can then use that table variable to filter your SQL query. 然后,您可以使用该表变量来过滤SQL查询。 I've given an example of how to use it below. 我在下面给出了如何使用它的示例。

In the example I create a table of rows with a start and end date. 在示例中,我创建了一个包含开始日期和结束日期的行表。 Then I filter that table to show only rows where a parameter value is between the start and end date. 然后,我对该表格进行过滤,以仅显示参数值在开始日期和结束日期之间的行。

DECLARE @testTable TABLE (Descript VARCHAR(25), startDate DATE, endDate DATE);
INSERT INTO @testTable (Descript, startDate, endDate)
    VALUES ('row1', '2014-05-01','2014-08-01'), ('row2', '2013-10-01','2014-01-10'), ('row3', '2015-10-01','2015-12-15'),('row4','2013-01-01','2015-01-01'),
        --these rows won't appear in the result set
            ('row5','2010-01-01','2010-06-01'), ('row6','2013-12-25','2014-05-20');


-- get all rows from the test table where a selected parameter value
-- is between the start and end dates.
SELECT *
FROM @testTable AS tbl
WHERE EXISTS (
    SELECT *
    FROM @dateValues
    WHERE val BETWEEN tbl.startDate AND tbl.endDate
);

In SSRS you can build tables and complex solutions. 在SSRS中,您可以构建表和复杂的解决方案。 In the Text Query of report builder here is an example of splitting apart a parameter to get three dates. 在报表构建器的文本查询中,这里是一个将参数拆分为三个日期的示例。

BEGIN 

/* suppose the inbound pram is a string with 10 places per date '01/01/2010,11/12/2012,05/06/2013' */
/* you could also nip the string apart by each comma... */

DECLARE @YEAR1 DATETIME 
DECLARE @YEAR2 DATETIME 
DECLARE @YEAR3 DATETIME

SET  @YEAR1 = CAST(SUBSTRING(@INBOUNDPARAM, 1, 10) AS DATETIME)
SET  @YEAR2 = CAST(SUBSTRING(@INBOUNDPARAM, 12, 10) AS DATETIME)
SET  @YEAR3 = CAST(SUBSTRING(@INBOUNDPARAM, 23, 10) AS DATETIME)

SELECT  @YEAR1 AS Year1, @YEAR2 AS Year2, @YEAR3 AS Year3

END

Of course the Year of the date is just YEAR(@Year1) = 2010 for example... 当然,例如,日期的年份仅为YEAR(@ Year1)= 2010 ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM