SSRS报告由存储过程\ IN运算符提供

时间:2014-11-17 14:07:49

标签: sql-server tsql sql-server-2008-r2 ssrs-2008-r2

我创建了一个报告来获取大量数据。该报告调用了一个名为sp_get52week的存储过程,它为我提供了有关客户的总计数据。该报告有3个参数,@ Year,@ Week,@ SalepersonNumber。 @SalespersonNumber是一个可以接受多个值的参数。只有一个SalesPersonNumber被选中时报告才有效,但不是多个。当我选择多个时,我收到错误:

An error has occurred during report processing. (rsProcessingAborted)
Query execution failed for dataset 'DataSet1'. (rsErrorExecutingCommand)
Error converting data type nvarchar to int.

报告背后的存储过程如下:

ALTER PROCEDURE [dbo].[sp_Get52Week]
    @Year INT,
    @Week INT,
    @SalesPersonNumber INT
AS
BEGIN
    SELECT  

a.[Group]
,a.[Owner]
,a.[OwnerLastName]
,SUM(a.[tot_sls_amt]) AS CYSales
,(b.[tot_sls_amt]) AS PYSales

  FROM 

[001].[MW].[MW_Submitted_Weeks_By_RBC_ROY] a
        Left Join 
            (SELECT sum([tot_sls_amt]) tot_sls_amt,[Group],[Owner] ,[SalesPersonNumber]
                 FROM [001].[MW].[MW_Submitted_Weeks_By_RBC_ROY]
                     WHERE ((Year = @Year-1 AND Week <= @Week) OR (Year = @Year - 2 AND Week > @Week)) AND cmp_status = 'A' AND [SalesPersonNumber] IN (@SalesPersonNumber)
                        group by [Group],[Owner] ,[SalesPersonNumber]) b on a.[Group] = b.[Group] and a.[Owner] = b.[Owner] and a.[SalesPersonNumber] =b.[SalesPersonNumber]



WHERE 
((Year = @Year AND Week <= @Week) OR (Year = @Year - 1 AND Week > @Week)) AND cmp_status = 'A' AND a.SalesPersonNumber IN (@SalesPersonNumber)


Group BY 
a.[Group] 
,a.[Owner]
,a.[OwnerLastName]
,(b.[tot_sls_amt])
END

如何使用IN运算符更改报表以允许使用存储过程的多个值?

2 个答案:

答案 0 :(得分:1)

我将此作为答案显示

CREATE FUNCTION [dbo].[sp_Get52Week] (
    @Year INT,
    @Week INT
)
RETURNS table AS
RETURN (
    SELECT a.[Group]
          ,a.[Owner]
          ,a.[OwnerLastName]
          ,a.[SalesPersonNumber]
          ,SUM(a.[tot_sls_amt]) AS CYSales
          ,(b.[tot_sls_amt]) AS PYSales
    FROM [001].[MW].[MW_Submitted_Weeks_By_RBC_ROY] a
         LEFT JOIN (SELECT sum([tot_sls_amt]) tot_sls_amt
                          ,[Group]
                          ,[Owner] 
                          ,[SalesPersonNumber]
                    FROM [001].[MW].[MW_Submitted_Weeks_By_RBC_ROY]
                    WHERE ((Year = @Year-1 AND Week <= @Week) OR 
                           (Year = @Year - 2 AND Week > @Week)) 
                          AND cmp_status = 'A' 
                    GROUP BY [Group]
                            ,[Owner] 
                            ,[SalesPersonNumber]) b 
             ON a.[Group] = b.[Group] 
                AND a.[Owner] = b.[Owner] 
                AND a.[SalesPersonNumber] = b.[SalesPersonNumber]
    WHERE ((Year = @Year AND Week <= @Week) 
           OR (Year = @Year - 1 AND Week > @Week)) 
          AND cmp_status = 'A' 
    GROUP BY a.[Group] 
            ,a.[Owner]
            ,a.[OwnerLastName]
            ,a.[SalesPersonNumber]
            ,(b.[tot_sls_amt])
)

在你做的报告中

SELECT * 
FROM [dbo].[sp_Get52Week](@Year, @Week)
WHERE [SalesPersonNumber] IN (@SalesPersonNumber)

答案 1 :(得分:0)

IN运算符不能在一个参数中使用多个值。此外,您的@SalesPersonNumber参数的数据类型为INT,只接受一个值。

要解决此问题,您可以将@SalesPersonNumber声明为VARCHAR(MAX)数据类型,然后将过滤器更改为以下内容:

WHERE ... AND 
    (',' + @SalesPersonNumber + ',' LIKE
     '%,' + CAST(a.SalesPersonNumber AS VARCHAR) + ',%')

这假定您将以逗号分隔的值列表分配给@SalesPersonNumber参数。您可能需要使用一些SSRS数组到字符串功能来实现此目的(请参阅SSRS JOIN函数)。

上面的代码基本上是做什么,检查表中的SalesPersonNumber是否匹配@SalesPersonNumber - 字符串中逗号分隔的数字之一。