0

我通过 Visual Studio 设置了一个非常基本的 SSRS 报告。到目前为止,我设置的参数没有“允许空白值”、“允许空值”和“允许多个值”,但我尝试使用它们没有运气。允许空白值和允许空值似乎根本不起作用。

在预览中,仍然弹出“请为参数‘xxxx’选择一个值。

我尝试在主数据集查询中处理“column = @column”与“column IN @column”查询。我还搜索了谷歌并将额外的数据集查询设置为“从表中选择不同的列,其中列不为空”我还尝试在查询中使用 UNION,它让我可以选择“NULL”作为选项,但是null 不包括所有内容,所以结果什么也没给我....

有谁知道解决方法或如何获取单个参数以在将其设置为全选的地方不需要选项?

任何帮助将不胜感激!谢谢你们!

-EDIT 这就是我的查询“SELECT DOCDATE, DOCID, CUSTNMBR, CUSTNAME, DOCAMNT FROM SOP30200 WHERE (DOCDATE BETWEEN @Fromdate AND @Todate) GROUP BY DOCDATE, DOCID, CUSTNMBR, CUSTNAME, DOCAMNT HAVING (CUSTNMBR = @Custnmbr OR @Custnmbr IS NULL) AND (DOCID IN (@DocID) OR @DocID IS NULL)”,但我仍然无法将 docID 部分设为空白。上面是什么,我仍然需要一个 SP 或者你下面的例子中的东西?

4

1 回答 1

2

通过设置 allow null 值,您表示您的数据集看起来像这样:

CREATE PROCEDURE myRSStoredProcedure
 @MyParameter varchar(50)=NULL
 --more code
 SELECT * FROM MyTable WHERE (MyField=@MyParameter OR @MyParameter IS NULL)

END

也就是说,NULL 是一个有效值,并且在您的存储过程中的某处被引用,请注意这部分WHERE (MyField=@MyParameter OR @MyParameter IS NULL)处理它的值或 NULL。如果你不包括OR @MyParameter IS NULL你所拥有的,WHERE MyField=@MyParameter那么考虑到你没有解决它们,你将如何期望 RS“允许 Nulls”。

对于允许多个值,这只是 Reporting Services 中允许进行多项选择的指示器。您仍然需要处理数据集中的逗号分隔值(您的存储过程),您不能只说:

SELECT * FROM MyTable WHERE ID IN (@MyParam)

它不是那样工作的。解决这个问题的一种方法是创建一个表值函数,该函数接受一个 CSV 列表并返回一个整数表。这是一个例子:

CREATE Function [dbo].[CsvToInt] ( @Array varchar(1000)) 
returns @IntTable table 
    (IntValue int)
--Parse comma seperated value parameters
--To be used in SELECT blah WHERE blah IN (...)
--This function returns int, but may be modified to return any datatype
AS
begin

    declare @separator char(1)
    set @separator = ','

    declare @separator_position int 
    declare @array_value varchar(1000) 

    set @array = @array + ','

    while patindex('%,%' , @array) <> 0 
    begin

      select @separator_position =  patindex('%,%' , @array)
      select @array_value = left(@array, @separator_position - 1)

        Insert @IntTable
        Values (Cast(@array_value as int))

      select @array = stuff(@array, 1, @separator_position, '')
    end

    return
end

最后,包含“所有”数据而不是空选择的一种巧妙方法是创建两个数据集,这意味着给我所有数据。第一个数据集是您的参数,例如对于您可能拥有的客户:

SELECT
  1 As SortOrder
  CustomerID,
  Customer
FROM
  MyCustomers
UNION ALL
SELECT
  0 As SortOrder
  0 As CustomerID,
  'All' As Customer
ORDER BY
  SortOrder,
  Customer

这将列出顶部带有单词 All 的所有客户(注意 SortOrder)。然后您的 RS 报告参数可以将默认值指定为 0(如果您使用的是 CustomerID)或All(如果您使用的是实际客户名称)。然后,您的报告数据集将执行以下操作:

CREATE PROCEDURE rsp_MainReport
@CustomerID int = 0  --this could also be @CustomerID varchar(50)='All'
--more code
SELECT Blah FROM MyTable WHERE (CustomerID = @CustomerID OR @CustomerID=0) 
--or you could of done WHERE (Customer = @Customer OR @Customer='All')

我将发布一个我为您完成的示例,这是一个参数数据集:

SELECT 
                    1 as SortOrder,
                    FacilityID,
                    Facility
                FROM
                    Facility
                UNION ALL
                SELECT
                    0 as SortOrder,
                    0 as FacilityID,
                    'All' as Facility
                ORDER BY 
                    SortOrder,
                    Facility

结果如下:

在此处输入图像描述

然后你可以在你的主数据集(你的报告的数据集)中使用它:

CREATE PROCEDURE rsp_QuikFixStats_ByFacilityCategory
    -- Add the parameters for the stored procedure here
    @FacilityID bigint=0,
    @StartDate datetime=NULL,
    @EndDate datetime=NULL

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT 
        f.Facility,
        c.Category, 
        COUNT(t.TicketID) AS TotalNumberOfTickets,
        SUM(CASE WHEN ClosedDate IS NULL AND TargetDate IS NOT NULL THEN 1 ELSE 0 END) AS TotalNumberOfOpenTickets,
        SUM(CASE WHEN ClosedDate IS NOT NULL THEN 1 ELSE 0 END) AS TotalNumberOfClosedTickets,
        SUM(CASE WHEN TargetDate IS NULL AND ClosedDate IS NULL THEN 1 ELSE 0 END) AS TotalNumberOfWaitForTickets,
        SUM(CASE WHEN TargetDate IS NOT NULL AND ClosedDate IS NULL AND TargetDate < GetDate() THEN 1 ELSE 0 END) AS TotalNumberOfOverdueTickets
    FROM 
        Ticket t
    INNER JOIN
        Facility f
    ON
        f.FacilityID = t.FacilityID
    INNER JOIN
        Category c
    ON
        c.CategoryID = t.CategoryID
    WHERE 
        (f.Inactive=0)
        AND (f.FacilityID = @FacilityID OR @FacilityID=0)
        AND (t.OpenDate >= @StartDate OR @StartDate IS NULL)
        AND (t.OpenDate <= @EndDate OR @EndDate IS NULL)
    GROUP BY
        f.Facility,
        c.Category
    ORDER BY
        f.Facility,
        COUNT(t.TicketID) DESC,
        c.Category      
END
GO

报告参数如下所示:

在此处输入图像描述

于 2012-09-19T19:51:51.160 回答