【问题标题】:Can I use @table variable in SQL Server Report Builder?我可以在 SQL Server 报表生成器中使用 @table 变量吗?
【发布时间】:2010-04-23 15:00:45
【问题描述】:

使用 SQL Server 2008 报告服务:

我正在尝试编写一个显示一些相关数据的报告,所以我想像这样使用@table 变量

DECLARE @Results TABLE (Number int
                       ,Name nvarchar(250)
                       ,Total1 money
                       ,Total2 money
                       )

insert into @Results(Number, Name, Total1)
select number, name, sum(total)
from table1
group by number, name

update @Results
set total2 = total
from
(select number, sum(total) from table2) s
where s.number = number

select from @results

但是,报表生成器不断要求输入变量@Results 的值。这可能吗?

编辑: 正如 KM 所建议的,我已经使用存储过程来解决我眼前的问题,但最初的问题仍然存在:我可以在报表生成器中使用 @table 变量吗?

【问题讨论】:

    标签: sql-server reporting-services report-designer


    【解决方案1】:

    没有。

    ReportBuilder 将

    1. 第二次猜你
    2. 将@Results 视为参数

    【讨论】:

    • 谢谢。我害怕这个答案:)
    【解决方案2】:

    将所有这些放在一个存储过程中,并让报告生成器调用该过程。如果您有很多行要处理,那么使用#temp 表可能会更好(在性能方面),您可以在其中在 Number 上创建一个聚集主键(或者它是 Number+Name,不确定您的示例代码)。

    编辑
    您可以尝试在一个 SELECT 中完成所有操作并将其发送给报告生成器,这应该是最快的(没有临时表):

    select
        dt.number, dt.name, dt.total1, s.total2
        from (select
                  number, name, sum(total) AS total1
                  from table1
                  group by number, name
             ) dt
            LEFT OUTER JOIN (select
                                 number, sum(total) AS total2
                                 from table2
                                 GROUP BY number --<<OP code didn't have this, but is it needed??
                            ) s ON dt.number=s.number
    

    【讨论】:

    • 谢谢。我考虑过这个选项,但仍然很好奇我是否可以使用@table 变量。
    【解决方案3】:

    我也看到了这个问题。似乎 SQLRS 有点区分大小写。如果您确保您的表变量在任何地方都以相同的字母大小写声明和引用,您将清除参数提示。

    【讨论】:

      【解决方案4】:

      您可以在 SSRS 数据集查询中使用表变量,例如在我的代码中添加所需的“空”记录以将组页脚保持在固定位置(示例使用 pubs 数据库):

      声明 @NumberOfLines INT 声明 @RowsToProcess INT 声明 @CurrentRow INT 声明@CurRow INT 声明 @cntMax INT 声明 @NumberOfRecords INT 声明@SelectedType char(12) DECLARE @varTable TABLE (# int, type char(12), ord int) DECLARE @table1 TABLE(类型 char(12)、title varchar(80)、ord int )
      DECLARE @table2 TABLE(类型 char(12)、title varchar(80)、ord int )

      插入@varTable SELECT count(type) as '#', type, count(type) FROM Titles GROUP BY type ORDER BY type SELECT @cntMax = max(#) from @varTable

      INSERT into @table1 (type, title, ord) SELECT type, N'', 1 FROM title INSERT into @table2 (type, title, ord) SELECT type, title, 1 FROM Titles

      SET @CurrentRow = 0 SET @SelectedType = N'' SET @NumberOfLines = @RowsPerPage

      从@varTable 中选择@RowsToProcess = COUNT(*)

      WHILE @CurrentRow SET @CurrentRow = @CurrentRow + 1

      SELECT TOP 1 @NumberOfRecords = ord, @SelectedType = type FROM @varTable WHERE type > @SelectedType SET @CurRow = 0 WHILE @CurRow < (@NumberOfLines - @NumberOfRecords % @NumberOfLines) % @NumberOfLines BEGIN SET @CurRow = @CurRow + 1 INSERT into @table2 (type, title, ord) SELECT type, '' , 2 FROM @varTable WHERE type = @SelectedType END END SELECT type, title FROM @table2 ORDER BY type ASC, ord ASC, title ASC

      【讨论】:

        【解决方案5】:

        为什么不能将两个结果集联合起来?

        【讨论】:

          【解决方案6】:

          使用表值函数而不是存储过程怎么样?

          【讨论】:

            【解决方案7】:

            这是可能的,只用'@@'声明你的表。示例:

            DECLARE @@results TABLE (Number int
                                   ,Name nvarchar(250)
                                   ,Total1 money
                                   ,Total2 money
                                   )
            
            insert into @@results (Number, Name, Total1)
            select number, name, sum(total)
            from table1
            group by number, name
            
            update @@results
            set total2 = total
            from
            (select number, sum(total) from table2) s
            where s.number = number
            
            select * from @@results
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-10-02
              • 2014-12-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-26
              相关资源
              最近更新 更多