reporting services—将单个参数的多个值从ssrs传递到配置单元

4urapxun  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(447)

在ssrs中创建值括在单引号中的串联字符串
以上问题有什么答案吗?。我也遇到了同样的问题:
ssrs端的查询是:

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

在本例中,数据源是一个配置单元表。用户对参数的选择是一个多值参数,我希望将其替换为:

where in ('value1','value2')

执行查询时。但是,当查看配置单元端的查询执行时,会发现:

where in ('value1,value2')

我怎样才能解决这个问题?

fiei3ece

fiei3ece1#

从这里的文档来看,配置单元查询语言似乎支持公共表表达式。
因此,类似于以下内容的工作应该是:

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

如果您不想在所有的数据集中都使用这个僵化的规则,那么您需要将这个逻辑封装到任何与SQLServer表值参数等价的配置单元中,也许是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;
nbewdwxp

nbewdwxp2#

我想出来了!为其他用户发布答案。
以如下表达式提供查询(在ssrs中的query下):
=“selectfrom xyz.test\u table1 where f1 in('”&join(参数)!参数值,“','”&“')”
上面的字符串操作转换为:
从xyz.test\u table1中选择
其中f1在('value1','value2')
注:value1,value2这里是用户选择的多值参数的值

相关问题