SSRS-多值参数字符串操作

时间:2019-03-13 00:11:58

标签: sql-server string tsql reporting-services ssrs-2008

上下文: 我有一个带有多值参数的SSRS报告。该报告将多值参数传递给存储过程,该存储过程将其分配给nvarchar类型的变量。该变量包含唯一的ID,这些ID映射到数据库中实际存储为int数据类型的值。

详细信息: 基本上我需要 a)修改ssrs报告以允许输入多值(我已经完成) b)将nvarchar变量拆分为可消耗的int类型片段 c)处理这些片段的查询 d)重新构造字符串,以根据用户要求以串联方式显示查询结果

问题: 您能协助我在以下草拟的程序中找到最有效的方法来执行这些转换吗?

旁注::虽然我发现了一些与在处理程序hereherehere中处理多值ssrs参数有关的帖子-考虑到需要进行多轮转换,这些修复程序不适用于我的特定情况。

/*This is the parameter supplied by the SSRS report that needs to be split, just declared here for commentary purposes
DECLARE @p_catalog_id nvarchar(max);
SET @p_catalog_id = '163650,163651,163652,163653,163654,163655,163656,163657,310415';
*/

BEGIN 

WITH EPV_IDs
AS
    (
        SELECT h.CATALOG_ID,
               h.PARENT_Catalog_ID as 'Series Collection ID',
               c.catalog_template as Template,
               d.CATALOG_ID as 'EPV ID'
        FROM [RDW_DIV38].[dbo].[Catalog_LINK_Catalog] as h 
          inner join [RDW_DIV38].[dbo].[Catalog] as c on c.Catalog_ID = h.CATALOG_ID
          left join [RDW_DIV38].[dbo].[Catalog_LINK_Catalog] as d on d.PARENT_Catalog_ID = h.PARENT_Catalog_ID
          inner join [RDW_DIV38].[dbo].[Catalog] as f on f.catalog_id=H.PARENT_Catalog_ID
        WHERE c.catalog_template = 'Episode Version'
          and f.catalog_template='series Collection'
          and c.Catalog_STATUS not in ('Withdrawn', 'Cancelled')
          and f.Catalog_STATUS not in ('Withdrawn', 'Cancelled')
          and d.Catalog_ID = @p_catalog_id
    ), 
X
AS
    (
        SELECT  EPV_IDs.[Series Collection ID],
                EPV_IDs.[Template],
                EPV_IDs.[EPV ID],
                COUNT(EPV_IDs.[EPV ID])as 'All EPVs'
        FROM EPV_IDs
        GROUP BY EPV_IDs.[Series Collection ID], EPV_IDs.[Template], EPV_IDs.[EPV ID]
    ),
y
AS
    (
        SELECT X.[Series Collection ID], 
               X.[Template],
               case when COUNT(X.[EPV ID]) = X.[All EPVs] Then 'Yes' Else 'No' ENd as 'Full?',
               COUNT(X.[EPV ID]) as 'EPVs Found',
               X.[All EPVs],STRING_AGG(X.[EPV ID],',')as 'EPV IDs'
        FROM X
        GROUP BY X.[Series Collection ID] ,X.[Template],x.[All EPVs]
    ), 
A
AS
    (
        SELECT x.[EPV ID],
               y.[Series Collection ID],
               y.[Template],
               STRING_AGG(y.[Full?],'/')as [Full?],
               y.[EPVs Found],
               y.[All EPVs],
               y.[EPV IDs]
        FROM y
        inner join x on x.[Series Collection ID]=y.[Series Collection ID]
        GROUP BY x.[EPV ID], y.[Series Collection ID], y.[Template], y.[EPVs Found], y.[All EPVs], y.[EPV IDs]
    )
        SELECT DISTINCT a3.[Series Collection ID] as Series_Collection_ID,
                        a3.[Template],
                        a.[Full?] as [Full],
                        a3.[EPVs Found] as EPVs_Found,
                        a3.[All EPVs] as All_EPVs,
                        a3.[EPV IDs] as EPV_IDs
        FROM a
        left join a as a3 on  a3.[EPV ID]=a.[EPV ID]
        WHERE not exists (

                           SELECT 1
                           FROM a a2
                           WHERE a2.[EPV ID] =a.[EPV ID] and  a2.[Full?] = 'Yes'

                         )
        UNION
        SELECT a.[Series Collection ID] as Series_Collection_ID,
               a.[Template],
               a.[Full?] as [Full],
               a.[EPVs Found] as EPVs_Found,
               a.[All EPVs] as All_EPVs,
               a.[EPV IDs] as EPV_IDs
        FROM a
        WHERE a.[Full?]='Yes'

END

1 个答案:

答案 0 :(得分:2)

如果您的数据库是2016,则可以只使用string_split: 替换

and d.Catalog_ID = @p_catalog_id

使用

and CAST(d.Catalog_ID as varchar(10)) IN (SELECT value FROM STRING_SPLIT(@p_catalog_id, ',')

否则,我建议您使用FOR XML PATH方法-那里有很多egs 和其他人: https://sqlperformance.com/2012/07/t-sql-queries/split-strings