Developer
Developer

Reputation: 245

While loop executing additional even after reaching the condition in TSQL

Below query is simple calculation on multiple tables and inserting the data based on condition to another table

DECLARE @CCODE nvarchar(30);
DECLARE @OHQNTY INT;
DECLARE @Itemid nvarchar(30);
DECLARE @ITemlookupcode nvarchar(30);
DECLARE @ExtDescription nvarchar(200);
DECLARE @Department nvarchar(50);
DECLARE @QtyRCV INT;
DECLARE @DAYSCOUNT INT;
DECLARE @DateRecive nvarchar(50);
DECLARE @PoNumber nvarchar(50);



DECLARE CUR CURSOR FOR
SELECT [Code],[Onhand] FROM [dbo].[vwStockOnHand] WHERE code <300000;

OPEN CUR

FETCH NEXT FROM CUR INTO @CCODE,@OHQNTY
WHILE @@FETCH_STATUS = 0   
BEGIN 
    WHILE(@OHQNTY>0)
    BEGIN
        SELECT TOP(1) @Itemid=[Itemid],@PoNumber=[PoNumber],@ITemlookupcode=[ITemlookupcode],
                    @ExtDescription=[ExtDescription],
                    @Department=[Department],@QtyRCV=[QtyRCV],@DateRecive=[DateRecive]
                    FROM [dbo].[StockIn]
                    WHERE ITemlookupcode=@CCODE AND [QtyRCV]>0
                    ORDER BY DateRecive DESC, PoNumber ASC;

        SET @DAYSCOUNT= DATEDIFF(DAY,MAX(@DateRecive),GETDATE());

        IF (@QtyRCV >= @OHQNTY AND @OHQNTY>0)
            BEGIN
                -- INSERTING DATA
                INSERT INTO [dbo].StockAging 
                (Itemid,ITemlookupcode,ExtDescription,Department,QtyRCV,DateRecive,DaysCount)
                VALUES(@Itemid,@ITemlookupcode,@ExtDescription,@Department,@OHQNTY,@DateRecive,@DAYSCOUNT)

                -- UPDATING  
                UPDATE  [dbo].[StockOnHand]
                SET [Onhand]=0 
                FROM [dbo].[StockOnHand]
                WHERE [Code]=@CCODE;

                UPDATE  [dbo].[StockIn]
                SET [QtyRCV]=0 
                FROM [dbo].[StockIn]
                WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

                SET @OHQNTY=0;
            END 
        ELSE IF (@OHQNTY > 0)
            BEGIN
                -- INSERTING DATA
                INSERT INTO [dbo].StockAging 
                (Itemid,ITemlookupcode,ExtDescription,Department,QtyRCV,DateRecive,DaysCount)
                VALUES(@Itemid,@ITemlookupcode,@ExtDescription,@Department,@QtyRCV,@DateRecive,@DAYSCOUNT) 

                -- UPDATING  
                UPDATE  [dbo].[StockOnHand]
                SET [Onhand]=[Onhand]-@QtyRCV FROM [dbo].[StockOnHand]
                WHERE [Code]=@CCODE;

                -- UPDATING  
                UPDATE  [dbo].[StockIn]
                SET [QtyRCV]=0 FROM [dbo].[StockIn]
                WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

                SET @OHQNTY=@OHQNTY-@QtyRCV;

            END
    END

    FETCH NEXT FROM CUR INTO @CCODE,@OHQNTY
END
    CLOSE CUR 
    DEALLOCATE CUR

I think the code is self explanatory ,

Issue I'm facing is even after reaching condition @OHQNTY>0 It is passing through the loop again. I know there is some issue with the looping, but couldn't find where I'm missing the logic. Here Resultis the result of the query, the below one with RecvdQty=9 is additional

Upvotes: 0

Views: 144

Answers (2)

MtwStark
MtwStark

Reputation: 4048

in IF (@QtyRCV >= @OHQNTY AND @OHQNTY>0)

you update

UPDATE  [dbo].[StockIn]
SET [QtyRCV]=0 
FROM [dbo].[StockIn]
WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

but you have "used" only @OHQNTY so I think you should

UPDATE  [dbo].[StockIn]
SET [QtyRCV]=[QtyRCV]- @OHQNTY 
FROM [dbo].[StockIn]
WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

also, you have your IF condition like that

IF (@QtyRCV >= @OHQNTY AND @OHQNTY>0) BEGIN
...
END ELSE IF (@OHQNTY > 0) BEGIN
...
END

but @OHQNTY > 0 is useless, it should be

IF (@QtyRCV >= @OHQNTY) BEGIN
...
END ELSE BEGIN
...
END

finally I will write it this way

IF (@QtyRCV >= @OHQNTY) BEGIN
  @QtyUsed = @OHQNTY
END ELSE BEGIN
  @QtyUsed = @QtyRCV
END

-- INSERTING DATA
INSERT INTO [dbo].StockAging 
(Itemid, ITemlookupcode, ExtDescription, Department, QtyRCV, DateRecive, DaysCount)
VALUES(@Itemid, @ITemlookupcode, @ExtDescription, @Department, @QtyUsed, @DateRecive, @DAYSCOUNT)

-- UPDATING  
UPDATE [dbo].[StockOnHand]
SET [Onhand] = [Onhand] - @QtyUsed
WHERE [Code]=@CCODE;

UPDATE [dbo].[StockIn]
SET [QtyRCV] = [QtyRCV] - @QtyUsed
WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

SET @OHQNTY = @OHQNTY - @QtyUsed;

Upvotes: 1

TheGameiswar
TheGameiswar

Reputation: 28890

I see issue in If Clause..

IF (@QtyRCV >= @OHQNTY AND @OHQNTY>0)
Begin
set @OHQNTY=0
End

When If Clause is not satisfied ,your while loop keeps on executing..move setting of @OHQNTY to outside of If Clause

Upvotes: 0

Related Questions