0

我有多个条件明智的存储过程,我想将结果集组合成单个过程......

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

以上两个过程通过处理和接收的摘要具有相同的结果计数,但是如何将它们组合到一个具有多个条件的查询?

4

1 回答 1

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
于 2013-03-01T14:15:31.827 回答