使用while循环获取详细信息时,我无法按照我的意愿获取它。我想要的是应用循环并获取每个日期的详细信息。我正在使用SQL 2008。
Set @i = 1;
While (@NoOfDays >= @i)
Begin
SET @ManufactureQty = (Select isnull(SUM(Quantity),0) as Manufactured from IGN1 inner join OIGN on (OIGN.DocEntry = IGN1.DocEntry)
inner join OITM on (OITM.ItemCode = IGN1.ItemCode)
where Convert(Date,Cast(OIGN.DocDate as DATE)) = Convert(Date,DATEADD(d, @i, @date)) and OITM.ItmsGrpCod = @ItemGroup)
Select @i as D,OpeningBalance = case
when @i > 1 then ((isnull(SUM(OINM.InQty),0)-isnull(SUM(OINM.OutQty),0)) + ((isnull(SUM(OINM.InQty),0)-isnull(SUM(OINM.OutQty),0))))
when @i = 1 then (isnull(SUM(OINM.InQty),0)-isnull(SUM(OINM.OutQty),0))
end
,
isnull(@ManufactureQty,0) as Manufacture
From OITM
Inner Join OINM On (OINM.ItemCode = OITM.ItemCode and Convert(Date,Cast(OINM.DocDate as DATE)) = Convert(Date,DATEADD(d, @i, @date))) --and Convert(Date,Cast(OINM.DocDate as DATE)) > Convert(Date,'2014-05-01'))
Where OITM.ItmsGrpCod = @ItemGroup group by OINM.DocDate
SET @i = @i + 1
End
It Gives
D OpeningBalance Manufacture
1 123 23
D OpeningBalance Manufacture
2 143 773
D OpeningBalance Manufacture
3 126 27
.
But I need it like
D OpeningBalance Manufacture
1 123 23
2 143 773
3 126 27
.
答案 0 :(得分:1)
之前循环:
create table #results (D int, OpeningBalance Int, Manufacture Int)
循环
begin
insert into #results ...
select ...
end
- 在
之后select * from #results