将单个参数的多个值从SSRS传递到配置单元

时间:2018-04-24 13:44:08

标签: reporting-services hive

Creating a concatenated string in SSRS with values enclosed in single quotes

上述问题的任何答案?我对同样的问题感到震惊:

SSRS方面的查询是:

select *
from xyz.test_table1
where f1 in (?)

在这种情况下,我的数据源是一个配置表。用户对参数的选择是一个多值参数,我希望将其替换为:

where in ('value1','value2')

执行查询时。但是当查看hive端的查询执行时,它会显示为:

where in ('value1,value2')

我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:0)

从文档here开始,Hive查询语言似乎支持公用表表达式。

因此,类似于以下的内容应该工作:

declare @str nvarchar(4000) = ?;        -- String to split.

with n(n) as (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1)

     -- Select the same number of rows as characters in @str as incremental row numbers.
     -- Cross joins increase exponentially to a max possible 10,000 rows to cover largest @str length.
  ,t(t)   as (select top (select len(isnull(@str,'')) a) row_number() over (order by (select null)) from n n1,n n2,n n3,n n4)

     -- Return the position of every value that follows the specified delimiter.
  ,s(s)   as (select 1 union all select t+1 from t where substring(isnull(@str,''),t,1) = ',')

     -- Return the start and length of every value, to use in the SUBSTRING function.
     -- ISNULL/NULLIF combo handles the last value where there is no delimiter at the end of the string.
  ,l(s,l) as (select s,isnull(nullif(charindex(',',isnull(@str,''),s),0)-s,4000) from s)

     -- Return each individual value in the delimited string along with it's position.
  ,v      as (select row_number() over(order by s) as rn
                    ,substring(@str,s,l) as item
              from l
             )
select *
from v
    join xyz.test_table1 as t
        on v.v = t.f1

如果您不希望在所有数据集中都不希望出现这种情况,那么您需要将此逻辑封装到SQL Server表值参数的Hive等价物中,可能是UDTF

在SQL Server中,该函数将定义如下:

create function [dbo].[fn_StringSplit4k]
(
  @str nvarchar(4000) = ' '        -- String to split.
 ,@delimiter as nvarchar(1) = ','  -- Delimiting value to split on.
 ,@num as int = null               -- Which value to return.
)
returns table
as
return
     -- Start tally table with 10 rows.
 with n(n)   as (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1)

     -- Select the same number of rows as characters in @str as incremental row numbers.
     -- Cross joins increase exponentially to a max possible 10,000 rows to cover largest @str length.
  ,t(t)   as (select top (select len(isnull(@str,'')) a) row_number() over (order by (select null)) from n n1,n n2,n n3,n n4)

     -- Return the position of every value that follows the specified delimiter.
  ,s(s)   as (select 1 union all select t+1 from t where substring(isnull(@str,''),t,1) = @delimiter)

     -- Return the start and length of every value, to use in the SUBSTRING function.
     -- ISNULL/NULLIF combo handles the last value where there is no delimiter at the end of the string.
  ,l(s,l) as (select s,isnull(nullif(charindex(@delimiter,isnull(@str,''),s),0)-s,4000) from s)

 select rn
          ,item
 from(select row_number() over(order by s) as rn
    ,substring(@str,s,l) as item
  from l
  ) a
 where rn = @num
  or @num is null;

答案 1 :(得分:0)

想出来!为其他用户发布答案。

提供查询(在SSRS中的Query下)作为如下表达式:

=“select * from xyz.test_table1其中f1 in('”& Join(Parameters!param.Value,“','”)&“')”

上述字符串操作转换为:

从xyz.test_table1中选择*其中f1 in('value1','value2')

注意:value1,value2这里是用户选择的多值参数

的值