Why my SQL Server 2008 query keep running? -


i have query in sql server 2008 r2 below, when execute query, keeps running...how debug find out going wrong code? idea pls. :)

declare @result table (      priority int,      partcode nvarchar(50),      orderqty int,       allocateqty int)  declare @order table(      priority int,      partcode nvarchar(50),      orderqty int)  declare @stock table(      partcode nvarchar(50),      stockqty int)   insert @order (priority,partcode,orderqty)  values(1,'a',10),            (2,'a',40);  insert @stock(partcode,stockqty)  values('a',22);  if (select sum(orderqty)from @order)<(select stockqty @stock) begin  insert @result(priority,partcode,orderqty,allocateqty)  select priority, partcode,orderqty,orderqty  @order end else begin declare @allocatedqty int = 0 declare @allocateqty int = 1 declare @runningstock int = (select stockqty @stock) while @runningstock>=0 begin     declare @priority int     select top 1 @priority = priority @order order priority asc     while @priority <= (select max(priority) @order)     begin         declare @orderqty int         select @orderqty = orderqty - @allocatedqty @order priority = @priority         select @allocateqty = case when @runningstock > 0 , @orderqty > 0 @allocateqty else 0 end         insert @result(priority,partcode,orderqty,allocateqty)         select @priority,                partcode,                 case when @orderqty >= 0 @orderqty else 0 end orderqty,                @allocateqty         @order          priority = @priority         set @priority += 1               set @runningstock = @runningstock - @allocateqty     end     set @allocatedqty += @allocateqty     if (@runningstock <= 0) break   end end; select priority,partcode,sum(allocateqty) [allocateqty] @result group priority,partcode 

your loop depends on @runningstock <= 0 terminate. yet tests shows @allocateqty evaluates 0! means "set @runningstock = @runningstock - @allocateqty" stops decrementing @runningstock. @ point in infinite loop. game over.

i used low tech method of

    print @runningstock     print @allocateqty 

near end of loop watch values.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -