我有多个条件明智的存储过程,我想将结果集合并到单个过程...
ALTER PROCEDURE [dbo].[usp_TotalReceivedUnits_Country]
@ContractCode NVARCHAR (20),
@CountryCode NVARCHAR (10),
@Years NVARCHAR(1000)
AS
BEGIN
SELECT ISNULL(SUM(S.NettoWeight), 0) TotalNettoWeight, COUNT(S.ShipmentID) Loads, ReceivalDateYear = YEAR(S.ReceivalDate)
FROM Shipment S
WHERE S.ContractCode = @ContractCode AND
S.[OrigCountryCode] = @CountryCode AND
S.ClosedYN = 0 AND
S.Shipped = 1 AND
S.DeletedYN = 0 AND
S.OutInbound = 2 AND
YEAR(S.ReceivalDate) IN (SELECT Data FROM dbo.fnSplitString(@Years, ','))
GROUP BY YEAR(S.ReceivalDate)
END
GO
ALTER PROCEDURE [dbo].[usp_TotalProcessedUnits_Country]
@ContractCode NVARCHAR (20),
@CountryCode NVARCHAR (10),
@Years NVARCHAR(1000)
AS
BEGIN
SELECT ISNULL(SUM(S.NettoWeight), 0) TotalNettoWeight, COUNT(S.ShipmentID) Loads, ReceivalDateYear = YEAR(S.ReceivalDate)
FROM Shipment S with (nolock)
WHERE S.ContractCode = @ContractCode AND
S.OrigCountryCode = @CountryCode AND
S.ClosedYN = 1 AND
S.Shipped = 1 AND
S.FinanceDetailsYN = 0 AND
S.DeletedYN = 0 AND
S.OutInbound = 2 AND
YEAR(S.ReceivalDate) IN (SELECT Data FROM dbo.fnSplitString(@Years, ','))
GROUP BY YEAR(S.ReceivalDate)
END
GO
以上两个程序按处理和收到的摘要具有相同的结果计数但如何将它们组合成一个具有多个条件的查询?
答案 0 :(得分:0)
如果我理解你的问题,你应该能够将输出加载到临时表中,然后选择:
CREATE TABLE #out1(TotalNettoWeight INT, Loads INT, ReceivalDateYear DATETIME2);
CREATE TABLE #out2(TotalNettoWeight INT, Loads INT, ReceivalDateYear DATETIME2);
GO
INSERT #out1
EXEC [dbo].[usp_TotalReceivedUnits_Country] 123,6,10;
INSERT #out2
EXEC [dbo].[usp_TotalProcessedUnits_Country] 123,6,10;
GO
SELECT * FROM #out1
UNION
SELECT * FROM #out2
GO
DROP TABLE #out1;
DROP TABLE #out2;
GO