我有存储过程A和B从不同的表中获取结果。这两个程序都有以下结果:
是否可以同时存储两个存储过程,然后按作者分组?关于我如何解决这个问题的任何指南或提示?
DECLARE @OverallResults AS TABLE
(
identifiers INT NOT NULL IDENTITY(1, 1),
sales_price INT,
quantity INT,
PUBLISHER VARCHAR
)
INSERT INTO @OverallResults (identifiers, sales_price, quantity,PUBLISHER)
EXEC reportItunes
INSERT INTO @OverallResults (identifiers, sales_price, quantity,PUBLISHER)
EXEC reportDDS
SELECT A.Publisher,
SUM([sales_price]) AS [TotalPrice],
SUM([Quantity] AS [TotalQuantity],
FROM @OverallResults A
GROUP BY A.Publisher
[Err] 42000 - [SQL Server]','附近的语法不正确。
答案 0 :(得分:0)
是的,您可以使用该结构创建表变量,并将两个SP的结果集插入表变量中:
DECLARE @OverallResults AS TABLE
(
Id INT NOT NULL IDENTITY(1, 1),
... Your other columns
)
INSERT INTO @OverallResults ([ItemID], [Price], [Quantity], [SUM], [Publisher])
EXEC StoredProcedure1
INSERT INTO @OverallResults ([ItemID], [Price], [Quantity], [SUM], [Publisher])
EXEC StoredProcedure2
SELECT A.Publisher,
SUM([Price]) AS [TotalPrice],
SUM([Quantity] AS [TotalQuantity],
SUM([Sum]) AS [TotalSum]
FROM @OverallResults A
GROUP BY A.Publisher
DECLARE @OverallResults AS TABLE
(
identifiers INT NOT NULL IDENTITY(1, 1),
sales_price INT,
quantity INT,
PUBLISHER VARCHAR(128)
)
INSERT INTO @OverallResults (sales_price, quantity,PUBLISHER)
EXEC reportItunes
INSERT INTO @OverallResults (sales_price, quantity,PUBLISHER)
EXEC reportDDS
SELECT A.Publisher,
SUM([sales_price]) AS [TotalPrice],
SUM([Quantity] AS [TotalQuantity]
FROM @OverallResults A
GROUP BY A.Publisher
然后您可以使用表变量进行聚合。
您的脚本已更正