1

更新:解决方案在答案的底部!

标题是:带有“WITH”的 SQL 查询在存储过程之外运行,但不是作为存储过程的主体

在 SQL Server 2008 上

工作 SQL 查询:

declare @start datetime = '01 jan 2012'
declare @end   datetime = '31 dec 2012'
declare @articleId int = 4

;WITH amonth(day) as
(
    select @start as day
        union all
    select day + 1
        from amonth
        where day < @end
)

(select total=(
    select SUM(UnitPrice * NewQuantity) as Value from InventoryBatch invBatches
    inner join
    (select * from
     (SELECT
          TOP 100 PERCENT
          [B].[InventoryBatchId] AS [InventoryBatchId],
          RANK() over (partition by [B].[InventoryBatchId] order by [B].[MutationDate] desc) AS [MutationDate],
          [B].[MutationDate] as [RealDate],
          [B].[Id],
          [B].NewQuantity
        FROM
          [InventoryBatchHistory] [B]
        WHERE
          ([B].[ArticleId] = @articleId AND [B].[DeliveryDate] <= amonth.day)
        GROUP BY
          [B].[InventoryBatchId], [MutationDate], [B].Id, B.NewQuantity) INV where MutationDate = 1) latest
          on invBatches.Id = latest.InventoryBatchId),
 amonth.day
    from amonth
    left join #t on #t.DT = amonth.day 
group by amonth.day ) OPTION (MAXRECURSION 400)

给我一个每天计算的总数的列表,很好。

现在我想将其作为 StoredProcedure 重用:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET NOCOUNT ON
GO

ALTER PROCEDURE [dbo].[GetTotalQuantityForArticleRange](
    @articleId INT,
    @start DATETIME,
    @end DATETIME)
AS
BEGIN

WITH amonth(day) as
(
    select @start as day
        union all
    select day + 1
        from amonth
        where day < @end
)

(select total=(
    select SUM(UnitPrice * NewQuantity) as Value from InventoryBatch invBatches
    inner join
    (select * from
     (SELECT
          TOP 100 PERCENT
          [B].[InventoryBatchId] AS [InventoryBatchId],
          RANK() over (partition by [B].[InventoryBatchId] order by [B].[MutationDate] desc) AS [MutationDate],
          [B].[MutationDate] as [RealDate],
          [B].[Id],
          [B].NewQuantity
        FROM
          [InventoryBatchHistory] [B]
        WHERE
          ([B].[ArticleId] = @articleId AND [B].[DeliveryDate] <= amonth.day)
        GROUP BY
          [B].[InventoryBatchId], [MutationDate], [B].Id, B.NewQuantity) INV where MutationDate = 1) latest
          on invBatches.Id = latest.InventoryBatchId),
 amonth.day
    from amonth 
    left join #t on #t.DT = amonth.day
group by amonth.day) OPTION (MAXRECURSION 400)

END



GO

称为:

declare @start1 datetime = '01 jan 2012'
declare @end1   datetime = '31 dec 2012'


exec GetTotalQuantityForArticleRange 4, @start = @start1, @end = @end1

我一直得到一个 NULL 结果。我错过了什么?

提前致谢。

根据 drdigit 的建议添加

使用时

WITH amonth(day) as ( select @start as day union all select day + 1 from amonth where day < @end ) SELECT * FROM amonth OPTION (MAXRECURSION 400); 

作为 SP 正文的第一行,我确实得到了我正在寻找的日期。在设置计算值时,我仍然无法理解这将如何不起作用。

到目前为止,所有感谢!

工作解决方案:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET NOCOUNT ON
GO

ALTER PROCEDURE [dbo].[GetTotalQuantityForArticleRange](
    @articleId INT,
    @start DATETIME,
    @end DATETIME)
AS
BEGIN

WITH amonth(day) as 
( 
    select @start as day
        union all
    select day + 1
        from amonth
        where day < @end
)
SELECT total=(
    select SUM(UnitPrice * NewQuantity) as Value from InventoryBatch invBatches
    inner join
    (select * from
     (SELECT
          TOP 100 PERCENT
          [B].[InventoryBatchId] AS [InventoryBatchId],
          RANK() over (partition by [B].[InventoryBatchId] order by [B].[MutationDate] desc) AS [MutationDate],
          [B].[MutationDate] as [RealDate],
          [B].[Id],
          [B].NewQuantity
        FROM
          [InventoryBatchHistory] [B]
        WHERE
          ([B].[ArticleId] = @articleId AND [B].[DeliveryDate] <= amonth.day)
        GROUP BY
          [B].[InventoryBatchId], [MutationDate], [B].Id, B.NewQuantity) INV where MutationDate = 1) latest
          on invBatches.Id = latest.InventoryBatchId), * FROM amonth OPTION (MAXRECURSION 400); 

END



GO
4

0 回答 0