WHILE @Suma < @ZalihaVP
begin
Insert into @Promet (Kljuc, Kolicina, Datum, StarostPrometa)
select top 1 m.acKey, anQTY, m.adDate, DATEDIFF(d, m.adDate, getdate())
from tHE_Move m inner join tHE_MoveItem mi
on m.acKey = mi.acKey
where acReceiverStock = 'Y'
and acReceiver = @Skladiste
and acIdent = @Ident
and m.acKey not in(select Kljuc from @Promet)
order by adDate desc
set @Suma = @Suma + (select SUM(kolicina) from @Promet)
if @Suma > @ZalihaVP begin update @Promet set Kolicina = Kolicina - (@Suma - @ZalihaVP) where KLjuc = (select top 1 KLjuc from @Promet order by Datum) end
if @Suma > @ZalihaVP begin set @Suma = @ZalihaVP end
end
这是我的WHILE循环,我在这里遇到问题。 @ZalihaVP = 92 @Suma = 0
@ promet.kolicina的记录是:40,50,50
在第三张唱片上,@ Suma比@ZalihaVP 40 + 50 + 50 = 140&gt; @Suma 和第三个记录没有插入,但我希望它们插入。
我如何发送@Suma&lt; @Zaliha插入后结束了吗?
答案 0 :(得分:1)
我认为问题在于这一行:
set @Suma = @Suma + (select SUM(kolicina) from @Promet)
您将@Suma
设置为自身加上所有kolicina
合并。
也许应该是:
set @Suma = select SUM(kolicina) from @Promet