我可以在 SQL Server 报表生成器中使用 @table 变量吗?

发布于 2024-08-30 08:50:01 字数 661 浏览 5 评论 0原文

使用 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 变量吗?

Using SQL Server 2008 Reporting services:

I'm trying to write a report that displays some correlated data so I thought to use a @table variable like so

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

However, Report Builder keeps asking to enter a value for the variable @Results. It this at all possible?

EDIT: As suggested by KM I've used a stored procedure to solve my immediate problem, but the original question still stands: can I use @table variables in Report Builder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉风有信 2024-09-06 08:50:01

不会。ReportBuilder

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

No.

ReportBuilder will

  1. 2nd guess you
  2. treats @Results as a parameter
人海汹涌 2024-09-06 08:50:01

将所有这些内容放入存储过程中,并让报表生成器调用该过程。如果您有很多行要处理,那么使用 #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

Put all of that in a stored procedure and have report builder call that procedure. If you have many rows to process you might be better off (performance wise) with a #temp table where you create a clustered primary key on Number (or would it be Number+Name, not sure of your example code).

EDIT
you could try to do everything in one SELECT and send that to report builder, this should be the fastest (no temp tables):

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
心舞飞扬 2024-09-06 08:50:01

我也见过这个问题。 SQLRS 似乎有点区分大小写。如果您确保表变量在各处都使用相同的字母大小写进行声明和引用,您将清除参数提示。

I've seen this problem as well. It seems SQLRS is a bit case-sensitive. If you ensure that your table variable is declared and referenced everywhere with the same letter case, you will clear up the prompt for parameter.

归属感 2024-09-06 08:50:01

您可以在 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 )

插入@varTable SELECT count(type) as '#', type, count(type) FROM 标题 GROUP BY 类型 ORDER BY 类型 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 设置@SelectedType = N'' SET @NumberOfLines = @RowsPerPage

SELECT @RowsToProcess = COUNT(*) from @varTable

WHILE @CurrentRow < @RowsToProcess 开始
设置@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

You can use Table Variables in SSRS dataset query like in my code where I am adding needed "empty" records for keep group footer in fixed postion (sample use pubs database):

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
与君绝 2024-09-06 08:50:01

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

Why can't you just UNION the two resultsets?

风尘浪孓 2024-09-06 08:50:01

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

How about using a table valued function rather than a stored proc?

帥小哥 2024-09-06 08:50:01

可以,只用“@@”声明您的表。例子:

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

It's possible, only declare your table with '@@'. Example:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文