数据未显示在SSRS报告中,但在SQL中执行查询显示数据

时间:2016-12-21 15:59:06

标签: sql-server tsql stored-procedures ssrs-2008

我想使用存储过程在报表中显示一些数据:

ALTER PROCEDURE [dbo].[usp_GetThirtyDaysSims]
(
    @DateFrom DATETIME = NULL,
    @DateTo DATETIME = NULL,
    @O2QuarterStartDate INT = NULL,
    @AreaId INT = NULL,
    @StoreId INT = NULL,
    @O2MonthStartDate INT = NULL,
    @TransactionType NVARCHAR(1000)
)
AS
BEGIN

    DECLARE @O2Quarter AS INT
    DECLARE @O2Year AS INT
    DECLARE @O2Month AS INT

    IF (@AreaId = 0 OR @AreaId = 999999999) 
    BEGIN
        SET @AreaId = NULL
    END

    IF (@StoreId = 0 OR @StoreId = 999999999) 
    BEGIN
        SET @StoreId = NULL
    END

    IF (@O2QuarterStartDate = 0 OR @O2QuarterStartDate = 999999999) 
    BEGIN
        SET @O2QuarterStartDate = NULL
    END

    IF (@O2MonthStartDate = 0 OR @O2MonthStartDate = 999999999) 
    BEGIN
        SET @O2MonthStartDate = NULL
    END

    IF(@O2QuarterStartDate IS NOT NULL) 
    BEGIN       
        SELECT @O2Quarter = O2Quarter,@O2Year = O2Year  
        FROM DimDate 
        WHERE DateKey=@O2QuarterStartDate
        SELECT @DateFrom = MIN([Date]),@DateTo = MAX([Date])  
        FROM DimDate 
        WHERE  O2Quarter=@O2Quarter AND O2Year=@O2Year      
    END

    IF(@O2MonthStartDate IS NOT NULL) 
    BEGIN
        PRINT @O2MonthStartDate
        SELECT @O2Month=O2Month, @O2Quarter = O2Quarter,@O2Year = O2Year  
        FROM DimDate 
        WHERE DateKey=@O2MonthStartDate
        SELECT @DateFrom = MIN([Date]),@DateTo = MAX([Date])  
        FROM DimDate 
        WHERE O2Month=@O2Month AND O2Quarter=@O2Quarter AND O2Year=@O2Year      
    END

    SELECT Area,StoreName,SUM(Tariff) SumOfTariff, COUNT(1) NoOfSimsSold
    FROM [dbo].[ufn_GetThirtyDaysSimsData](@DateFrom,@DateTo) FT
           INNER JOIN DimDate DD ON FT.TransactionDateId = DD.DateKey
    WHERE (@DateFrom IS NOT NULL AND DD.[Date] >= @DateFrom)
          AND (@DateTo IS NOT NULL AND DD.[Date] <= @DateTo)
          AND (FT.AreaId = @AreaId OR @AreaId IS NULL)
          AND (FT.StoreId = @StoreId OR @StoreId IS NULL)
          AND (FT.TransactionType = @TransactionType OR @TransactionType IS NULL)
    GROUP BY Area,StoreName
    ORDER BY Area,StoreName

END

当我使用上面的7个过滤器进行过滤时,报告中没有显示数据,但也没有错误 - 为什么会这样?

1 个答案:

答案 0 :(得分:0)

确保您的功能是基于表格的。您不需要在WHERE子句中添加日期参数,因为您已经在函数中传递了这些参数。首先在SSMS中运行查询来测试您的查询。

以下是查询的简化版本:

SELECT Area, StoreName, SUM(Tariff) SumOfTariff, COUNT(1) NoOfSimsSold
FROM [dbo].[ufn_GetThirtyDaysSimsData](@DateFrom,@DateTo) FT
      INNER JOIN DimDate DD ON FT.TransactionDateId = DD.DateKey
WHERE FT.AreaId = @AreaId
      AND FT.StoreId = @StoreId
      AND FT.TransactionType = @TransactionType
GROUP BY Area, StoreName
ORDER BY Area, StoreName