即使SSRS报告中的表中没有数据,我们可以将相同数量的输出作为空白吗?

时间:2014-10-16 04:53:26

标签: sql-server-2008 ssrs-2008

我有一份报告,我在一份报告中将一些ID作为参数传递。

我的要求是如果我传递10个Id作为参数,我应该在报告中获得10条记录。 假设对于ID 3,表中没有数据。然后我应该在报告中获得带有空白列的ID 3。

OR

我们可以在报告中放置一个文本框,其中包含这些ID的无数据信息

如果我们能够以第一种方式做,那将非常有用。

提前致谢

此致 sshh0988

1 个答案:

答案 0 :(得分:0)

您可以尝试获取参数列表并设置一个数据集,该数据集将在表格中将它们返回给您并在结果集中使用此表格,这样您就可以将结果LEFT OUTER JOIN连接到提供的参数列表。像这样:

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. SSRS 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

现在,您提供的每个参数的数据集总是会有一行,只有返回匹配值的值。 SSRS默认提供多选参数作为逗号分隔列表,如上所示。当然,null返回值可以按照您的意愿处理

对于您的选项B,可以输入&#34;无数据消息&#34;对于表格或地区 - 请参阅this link for more info