>>>Too bad that your lowest version is SQL Server 2008 because otherwise it would be a simpler solution.
>>>
>>
>>What version of SQL Server I should ask the customers to upgrade to, in order to make this a simpler solution?
>
>2012 version of SQL Server has window functions that may be helpful (SUM over (ORDER BY ...))
Thank you.
One more question (hopefully, for your sake :), last one for today. Your code (below) works.
declare @QtyUse as Int;
set @QtyUse = 1;
update t1 set t1.qty_left = t1.qty_left - @QtyUse
from myTable t1
inner join
(select top (1) t2.pk from MyTable t2 where t2.rec_id = '1'
order by t2.date_rec) t2 on t1.pk = t2.pk
The only thing is, how do I determine that no record is found in the Select top(1) clause? So, that I can exit the While?
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham