更新:解決策は答えの一番下にあります!
タイトルは次のとおりです。「WITH」を使用したSQLクエリはストアドプロシージャの外部で実行されますが、ストアドプロシージャの本体としては実行されません
SQLServer2008の場合
動作する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