hi all, ive no idea what's wrong with my while loop nested in IF .. it only work correctly when i remove the while from IF :-IF @Picktype='FI'BEGIN -- Insert data into @Stage to play around DECLARE @Stage TABLE (RecID INT IDENTITY(1, 1), ItemStorageID VARCHAR(12), Qty MONEY, RecvDate DATETIME,BB char(1)) WHILE (SELECT COALESCE(SUM(Qty), 0) FROM @Stage) < @WantedValue AND @@ROWCOUNT > 0 INSERT @Stage (ItemStorageID, Qty, RecvDate, BB) SELECT TOP 1 t1.ItemStorageID, t1.Qty, t1.RecvDate, t1.BB FROM #DataList AS t1 LEFT JOIN @Stage AS s ON s.ItemStorageID = t1.ItemStorageID WHERE s.ItemStorageID IS NULL ORDER BY t1.RecvDate, t1.Qty DESC IF (SELECT COALESCE(SUM(Qty), 0) FROM @Stage) >= @WantedValue SELECT recID, ItemStorageID, Qty, RecvDate, BB FROM @Stage ELSE select * from #DataListEND
correct result after i remove the while from inside IFDECLARE @Stage TABLE (RecID INT IDENTITY(1, 1), ItemStorageID VARCHAR(12), Qty MONEY, RecvDate DATETIME,BB char(1)) WHILE (SELECT COALESCE(SUM(Qty), 0) FROM @Stage) < @WantedValue AND @@ROWCOUNT > 0 INSERT @Stage (ItemStorageID, Qty, RecvDate, BB) SELECT TOP 1 t1.ItemStorageID, t1.Qty, t1.RecvDate, t1.BB FROM #DataList AS t1 LEFT JOIN @Stage AS s ON s.ItemStorageID = t1.ItemStorageID WHERE s.ItemStorageID IS NULL ORDER BY t1.RecvDate, t1.Qty DESC IF @pickType='FI' BEGIn IF (SELECT COALESCE(SUM(Qty), 0) FROM @Stage) >= @WantedValue SELECT recID, ItemStorageID, Qty, RecvDate, BB FROM @Stageend
~~~Focus on problem, not solution~~~