通过设置 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
报告参数如下所示: