即使数据不在 s-s-rS 报告中的表格中,我们能否获得与空白相同数量的输出?
Posted
技术标签:
【中文标题】即使数据不在 s-s-rS 报告中的表格中,我们能否获得与空白相同数量的输出?【英文标题】:Can we have the same number of output as blank even the data is not there in Table in s-s-rS report? 【发布时间】:2014-10-16 04:53:26 【问题描述】:我有一份报告,其中我在一份报告中传递了一些 ID 作为参数。
我的要求是如果我传递 10 个 ID 作为参数,我应该在报告中获得 10 条记录。 假设 ID 3 的数据不存在于表中。那么我应该在报告中获得 ID 3 的空白列。
或
也许我们可以在报告中放置一个文本框,其中包含类似 No data for these ID's 这样的消息
如果我们能做到第一种方式,那将非常有帮助。
提前致谢
问候 sshh0988
【问题讨论】:
了解如何正确发布您的问题。提供有关您的表格的信息,提供您正在处理的代码以及您想要实现的结果。 【参考方案1】:您可以尝试获取参数列表并设置一个数据集,该数据集将在表格中将它们返回给您,并在结果集中使用此表格,这样您就可以将结果左外连接到提供的参数列表中。像这样的:
DECLARE @MyParameters VARCHAR(Max)
DECLARE @Separator VARCHAR(10)
SET @MyParameters = '1,2,3,4,5,6,7,8,9,10'
SET @Separator = ','
DECLARE @Found as VARCHAR(MAX)
DECLARE @TableList TABLE (Value NVARCHAR(MAX))
SET @Separator = ISNULL(@Separator,',')
-- Check whether the @MyParameters parameter is a single value or a concatenated list. s-s-rS will deliver a comma seperated list if
-- more than one organisation is selected
IF CHARINDEX(@Separator,@MyParameters) <> 0
-- If the @MyParameters is actually a concatenated list, we need to split it out into seperate names, which is achieved with a loop:
BEGIN
RinseAndRepeat:
-- If there are no more seperators in the list then we must have got to the last value in the list
IF CHARINDEX(@Separator,@MyParameters) = 0
BEGIN
-- Set our most recent value to whatever remains in the list (i.e. the last value in the list)
SET @Found = @MyParameters
-- Add the value to the table containing all values from the list
INSERT INTO @TableList VALUES (@Found)
END
-- If there is still at least one seperator in the list, there is more than one value left, so we need to take one off the front of the list
ELSE
BEGIN
IF LEFT(@MyParameters,LEN(@Separator)) = @Separator
-- If there is a seperator on the front of the remaining list, take it off.
SET @MyParameters = RIGHT(@MyParameters, LEN(@MyParameters)-LEN(@Separator))
-- Take from the start of the list up to the next seperator (not including the seperator itself)
SET @Found = SUBSTRING(@MyParameters,0,CHARINDEX(@Separator,@MyParameters))
-- Update the list to not include the most recently taken value, nor the seperator that follows it
SET @MyParameters = SUBSTRING(@MyParameters,LEN(@Found)+ 1 + LEN(@Separator),LEN(@MyParameters))
-- Add the found value to the table of all found values
INSERT INTO @TableList VALUES (@Found)
-- Go back and look at the next term, which is now at the start of the list
GOTO RinseAndRepeat
END
END
-- If the @MyParameters parameter is not a concatenated list, then just take that name and put it in the name list table.
ELSE
BEGIN
INSERT INTO @TableList VALUES(@MyParameters)
END
------------------------------------------------------------------
-- whatever you do to get your results
DECLARE @Results TABLE (Id INT NOT NULL, Value VARCHAR(10))
INSERT @Results VALUES (1,'One')
INSERT @Results VALUES (2,'Two')
INSERT @Results VALUES (6,'Six')
INSERT @Results VALUES (9,'Nine')
INSERT @Results VALUES (10,'Ten')
SELECT MyParam.Value AS Id , Results.Value
FROM
@TableList MyParam
LEFT OUTER JOIN @Results Results ON MyParam.Value = Results.Id
现在,您的数据集将始终为每个提供的参数保留一行,并且仅在返回匹配值时才包含值。默认情况下,s-s-rS 会以逗号分隔列表的形式提供多选参数,如上所示。当然,null返回值可以随意处理
对于您的选项 B,可以为表格或区域输入“无数据消息” - 请参阅 this link for more info。
【讨论】:
以上是关于即使数据不在 s-s-rS 报告中的表格中,我们能否获得与空白相同数量的输出?的主要内容,如果未能解决你的问题,请参考以下文章