如何使用uniqueidentifier创建多选参数?

问题描述 投票:2回答:2

我正在开发SSRS 2008 R2 RDL文件。现在,我正在尝试添加一个应该是多选的报表参数。以前我使用过多选参数,但它们之前一直是CHARACTER数据类型。所以在我的主数据集中,我的代码是:

;WITH Cte AS 
( 
    SELECT 
        1 as id, 
        CAST('<M>' + REPLACE( (select @IncidentType),  ',' , '</M><M>') + '</M>' AS XML) AS Names 
) 
SELECT 
    id, 
    Split.a.value('.', 'VARCHAR(MAX)') AS Names
INTO #incident_types     
FROM Cte 
CROSS APPLY Names.nodes('/M') Split(a)

(在我的RDL文件中,此IncidentType报告参数允许多选)

但现在事件类型是UNIQUEIDENTIFIER数据类型。我已经尝试过SSRS JOIN函数但是当我运行它时,这仍然在SSRS中给出了同样的错误:

An error has occurred during report processing. (rsProcessingAborted)
Query execution failed for dataset 'Incidents'. (rsErrorExecutingCommand)
Conversion failed when converting from a character string to uniqueidentifier. 

如何通过传入多个uniqueidentifier来编写代码?

我尝试了你发布的下面的解决方案:

CAST(Split.a.value('.', 'VARCHAR(MAX)') AS UNIQUEIDENTIFIER) AS Names

但这给了我这个错误:

Msg 8169, Level 16, State 2, Line 62
Conversion failed when converting from a character string to uniqueidentifier.

在我的sproc顶部,我有:

declare
    @FacilityID varchar(MAX) = null,
    @ProgramID uniqueidentifier = null,
    @ServiceDateStart smalldatetime = null,
    @ServiceDateEnd smalldatetime = null,
    @IncidentType varchar(MAX) = null

SET @IncidentType = '864EA130-F36B-4958-B51F-EE9DBD35D804,671A8EB3-8298-40DB-BD66-93309904E463,ACA1EB55-3D66-467B-8388-CC42FCFB00F3
SET @FacilityID = '83B465B8-28DD-4F37-9F2D-A4D5E38EE7FB,3EC657F7-655F-43FB-8424-2A8914E7C725,B2064474-5C9B-4884-B1D7-4FCE1C804DF7'

但导致此错误的行是:

   AND (@IncidentType is NULL OR event_definition_rv.event_definition_id in (SELECT Names FROM #incident_types))

因为我可以运行这些行没有错误:

select * from #incident_types
select * from #facilities
reporting-services parameters uniqueidentifier
2个回答
1
投票

尝试将unsplit值转换为uniqueidentifier:

    ;WITH Cte AS 
( 
    SELECT 
        1 as id, 
        CAST('<M>' + REPLACE( (select @IncidentType),  ',' , '</M><M>') + '</M>' AS XML) AS Names 
) 
SELECT 
    id, 
    CAST(Split.a.value('.', 'VARCHAR(MAX)') AS UNIQUEIDENTIFIER) AS Names
INTO #incident_types
FROM Cte 
CROSS APPLY Names.nodes('/M') Split(a)

0
投票

但导致此错误的行是:

AND (@IncidentType is NULL OR event_definition_rv.event_definition_id in (SELECT Names FROM #incident_types))

由于@IncidentType是一个多值报表参数,因此您不能在报表的数据集查询中使用@IncidentType Is NULL,因为它会弄乱sql代码并且变得不可预测。

另外“多值参数不能包含空值”,因此检查参数是否为空是没用的。你可以像这样使用possible workaround

 AND ('NULL_FLAG' IN (@IncidentType) OR event_definition_rv.event_definition_id IN (@IncidentType))

请参阅docs.microsoft.com,其中说:

  • 查询必须使用IN子句指定参数。

注意:

报表服务器重写对无法将参数作为数组处理的数据源的查询。重写查询是产生预期结果所必需的。当参数定义为多值并且查询使用IN语句指定参数时,将触发查询重写。如果您构建的查询不包含IN语句,请注意您正在规避报表服务器提供的逻辑以支持多值参数。

© www.soinside.com 2019 - 2024. All rights reserved.