2010-04-23 82 views
3

使用SQL Server 2008报表服务:我可以在SQL Server Report Builder中使用@table变量吗?

我想,所以我想用一个变量@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变量吗?

回答

4

报表制作会

  1. 第二猜你
  2. 对待@Results作为参数
+0

谢谢。我很害怕这个答案:) – edosoft 2010-08-05 07:56:23

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 
+0

谢谢。我考虑过这个选择,但是如果我能够使用@table变量,我还是很好奇。 – edosoft 2010-04-23 15:17:50

0

为什么你就不能UNION两个结果集?

0

如何使用表值函数,而不是一个存储过程?

1

你可以在我的代码在那里我加入了保组页脚需要“空”记录在固定现在的位置是使用在SSRS集查询表变量,如(示例使用pubs数据库):

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

INSERT INTO @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 titles INSERT into @table2 (type, title, ord) SELECT type, title, 1 FROM titles

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

SELECT @RowsToProcess = COUNT(*) from @varTable

WHILE @CurrentRow < @RowsToProcess BEGIN
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
0

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

相关问题