Skip to content
Advertisement

How to use WHILE LOOP to add value to list with condition, SQL Server 2008

I have a loop query with scenario below:

  • orderqty = increase value 1 each loop
  • runningstock = decrease value 1 each loop
  • allocateqty = case when orderqty > 0 and runningstock > 0 then 1 else 0
  • loop till runningstock=0 or total allocation=stockqty

Query:

Result:

the correct one should be like this:

Advertisement

Answer

You can also loop through your priorities and make sure the orderqty is updated accordingly. I’d do something like this:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement